LINQ to Lookup with distinct items and count - c#

If I define a list as
public class ItemsList
{
public string structure { get; set; }
public string Unit { get; set; }
public double Dim { get; set; }
public double Amount { get; set; }
public int Order { get; set; }
public string Element { get; set; }
}
List<ItemsList> _itemsList = new List<ItemsList>();
I'm trying to get the a distinct count of structures in a Lookup with structure as the key and count of structure as the value.
At the moment I have
var sCount = from p in _itemsList
group p by p.Structure into g
select new { Structure = g.Key, Count = g.Count() };
but this just returns the data as an anonymous type. Can someone please help me with the syntax to get this into a Lookup using .ToLookup?

I suspect you actually want:
var lookup = _itemsList.ToLookup(p => p.Structure);
You can still count the items for any group:
foreach (var group in lookup)
{
Console.WriteLine("{0}: {1}", group.Key, group.Count());
}
... but you've also got the values within each group.
If you really just want the count, then it sounds like you don't want a lookup at all - you want a Dictionary, which you can get with:
var dictionary = _itemsList.GroupBy(p => p.Structure)
.ToDictionary(g => g.Key, g => g.Count());

Related

Getting Sum by looping a list within a list c#

public class GRNMaster
{
public string ID { get; set; }
public string GRNNo { get; set; }
public List<GRNDetails> GRNDetails { get; set; }
}
public class GRNDetails
{
public string GRNID { get; set; }
public string ItemID { get; set; }
public string ItemType { get; set; }
public int RecevedQty { get; set; }
}
above classes contains some of the properties of GRN header class and Detail class. i Grn can consist of many items so that "List GRNDetails" is there to keep them.
I take a GRN List from a method which will store in the variable GrnList
public List<GRNMaster> GrnList
I have a list of Items IDs
public List<string> ItemIDList
In the controller I want to loop the ItemIDList (List ItemIDList) and get sum for that particular item based on the List
int ItemQty = 0;
foreach (var item in ItemIDList)
{
ItemQty = 0;
var ItemQty = //some code using GrnList
// rest of the programming code based on the
// item qty
}
Using LINQ
var totalQty = 0;
foreach (var item in ItemIDList)
{
var sumOfCurrentItem = GrnList.SelectMany(s => s.GRNDetails
.Where(f => f.ItemID == item)).Select(f => f.RecevedQty).Sum();
totalQty += sumOfCurrentItem ;
}
Or even a one liner replacement of the foreach loop (Credit goes to ReSharper :) )
int totalQty = ItemIDList.Sum(item => GrnList
.SelectMany(s => s.GRNDetails.Where(f => f.ItemID == item))
.Select(f => f.RecevedQty).Sum());
If I've understood your requirements correctly then this is what you need:
IEnumerable<int> query =
from grn in GrnList
from grnDetail in grn.GRNDetails
join itemID in ItemIDList on grnDetail.ItemID equals itemID
select grnDetail.RecevedQty;
int ItemQty = query.Sum();

Group IEnumerable into a string

I wonder if someone could spare me a few minutes to give me some advice please?
I've created an IEnumerable list:
public class EmailBlock
{
public int alertCategory { get; set; }
public string alertName { get; set; }
public string alertURL { get; set; }
public string alertSnippet { get; set; } //Need to work out the snippet
}
List<EmailBlock> myEmailData = new List<EmailBlock>();
Which I then loop through some data (Umbraco content - not that that's really relevant!) and add items to the list.
myEmailData.Add(new EmailBlock { alertCategory = category.Id, alertName = alert.GetPropertyValue("pageTitle"), alertURL = alert.NiceUrl });
What ultimately I'd like to do is group the list by the alertCategory and then load each 'group' (another loop occurs later to check what members have subscribed to what alert category) into a variable which I can then use as an email's content.
You could use Linq's GroupBy() to do this:
using System.Linq
...
//Create a type to hold your grouped emails
public class GroupedEmail
{
public int AlertCategory { get; set; }
public IEnumerable<EmailBlock> EmailsInGroup {get; set; }
}
var grouped = myEmailData
.GroupBy(e => e.alertCategory)
.Select(g => new GroupedEmail
{
AlertCategory = g.Key,
EmailsInGroup = g
});
You can select to an anonymous type if required and project your sequence into whatever structure you require.
Linq has a nice group by statement:
var emailGroup = emailList.GroupBy(e => e.alertCategory);
Then you can loop through each grouping and do whatever you want:
foreach(var grouping in emailGroup)
{
//do whatever you want here.
//note grouping will access the list of grouped items, grouping.Key will show the grouped by field
}
Edit:
To retrieve a group after you have grouped them, just use Where for more than one or First for just one:
var group = emailGroup.First(g => g.Key == "name you are looking for");
or
var groups = emailGroup.Where(g => listOfWantedKeys.Contains(g.Key));
this is a lot more efficient than looping through every time you need to find something.

How to use group by on multiple columns with Max function

Here is a sample table, in which I am going to extract the records with the highest priority, corresponding to each pair of ID and code, as below:
Here is my approach to hit the mark:
var max = from item in items
group item by new {item.code, item.id} into r
select new MyObjectType(r.Select(q => q.code),
r.Select(q => q.id),
r.Max(q => q.priority));
But the result is null...
Any idea to fix the problem?!
Edit:
Here is a brief example:
(code,id,priority)
(1,10,100)
(1,10,200)
(1,11,300)
(1,11,400)
(2,12,500)
(2,12,600)
(2,13,700)
(2,13,800)
And the result of the query should be:
(1,10,200)
(1,11,400)
(2,12,600)
(2,13,800)
Make public properties in class and do like this:
var max = from item in items
group item by new {item.code, item.id} into r
select new MyObjectType
{
Code = r.Key.code,
Id = r.Key.id,
MaxValue = r.Max(q => q.priority)
};
Your class should look like:
public class MyObjectType
{
public int Code { get; set; }
public int Id { get ; set; }
public int MaxValue { get; set; }
}

LINQ query for retrieving data from list

I have List collection of Message objects.
public class Message
{
public int Id { get; set; }
public string Body { get; set; }
public string Sender { get; set; }
public DateTime Timestamp { get; set; }
}
I want to get only one message with most recent Timestamp for each sender. How do I do it using LINQ?
You need to group by Sender and then get the Max Timestamp from each group like:
var query = list.GroupBy(r => r.Sender)
.Select(grp => new
{
Sender = grp.Key,
RecentTimeStamp = grp.Max(r => r.Timestamp)
});
Or you can sort the TimeStamp in group by descending order and get the first element like:
var query = list.GroupBy(r => r.Sender)
.Select(grp => new
{
Sender = grp.Key,
RecentTimeStamp = grp.OrderByDescending(r => r.Timestamp).FirstOrDefault()
});
var q = from n in table
group n by n.Senderinto g
select g.OrderByDescending(t=>t.Timestamp).FirstOrDefault();

IList<T> in multi map/reduce result?

I'm struggling with RavenDB's multi map/reduce concept and recently asked this question regarding how to properly write a multi map/reduce index.
I got the simple index in that question working but when I tried to make it a bit more complicated, I cannot make it work. What I want to do is to have the result of the index to contain a list of string, i.e:
class RootDocument {
public string Id { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
public IList<string> Items { get; set; }
}
public class ChildDocument {
public string Id { get; set; }
public string RootId { get; set; }
public int Value { get; set; }
}
class RootsByIdIndex: AbstractMultiMapIndexCreationTask<RootsByIdIndex.Result> {
public class Result {
public string Id { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
public IList<string> Items { get; set; }
public int Value { get; set; }
}
public RootsByIdIndex() {
AddMap<ChildDocument>(
children => from child in children
select new {
Id = child.RootId,
Foo = (string)null,
Bar = (string)null,
Items = default(IList<string>),
Value = child.Value
});
AddMap<RootDocument>(
roots => from root in roots
select new {
Id = root.Id,
Foo = root.Foo,
Bar = root.Bar,
Items = root.Items,
Value = 0
});
Reduce =
results => from result in results
group result by result.Id into g
select new {
Id = g.Key,
Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(),
Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),
Items = g.Select(x => x.Items).Where(
x => x != default(IList<string>).FirstOrDefault(),
Value = g.Sum(x => x.Value)
};
}
}
Basically I tried to set the Items property to default(IList) when mapping the ChildDocuments and to value of the RootDocument's Items property. This doesn't work, however. It gives the error message
Error on request Could not understand query:
-- line 2 col 285: invalid Expr
-- line 2 col 324: Can't parse double .0.0
when uploading the index. How do I handle lists in multi map/reduce indexes?
Don't use List in your indexes, instead, use arrays.
AddMap<ChildDocument>(
children => from child in children
select new {
Id = child.RootId,
Foo = (string)null,
Bar = (string)null,
Items = new string[0],
Value = child.Value
});
AddMap<RootDocument>(
roots => from root in roots
select new {
Id = root.Id,
Foo = root.Foo,
Bar = root.Bar,
Items = root.Items,
Value = 0
});
Reduce =
results => from result in results
group result by result.Id into g
select new {
Id = g.Key,
Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(),
Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),
Items = g.SelectMany(x=>x.Items),
Value = g.Sum(x => x.Value)
};
David, you need to understand that RavenDB stores its indexes with Lucene.NET. That means, you cannot have any complex .net type inside your index.
In your example, I suggest you use a simple string instead of IList<string>. You can then join your string-items:
AddMap<ChildDocument>(
children => from child in children
select new {
Id = child.RootId,
Foo = (string)null,
Bar = (string)null,
Items = (string)null,
Value = child.Value
});
AddMap<RootDocument>(
roots => from root in roots
select new {
Id = root.Id,
Foo = root.Foo,
Bar = root.Bar,
Items = string.Join(";", root.Items),
Value = 0
});

Categories