c# loop on group by key - c#

I have made a group by statement on a datatable like this:
var finalResult = (from r in result.AsEnumerable()
group r by new
{
r.Agent,
r.Reason
} into grp
select new
{
Agent = grp.Key.Agent,
Reason = grp.Key.Reason,
Count = grp.Count()
}).ToList();
The finalResult will be like this:
agent1 reason1 4
agent1 reason2 7
agent2 reason1 8
agent2 reason2 3
..
...
...
agentn reason1 3
agentn reason2 11
I want to loop over agent name in order to get the reasons and the counts for each reason for each agent. In other words: i need to build this :
can you tell me please how to loop over agent name from the finalResult variable?

You need one more GroupBy and you are done:
var solution =
finalResult
.GroupBy(x => x.Agent);
foreach (var group in solution)
{
// group.Key is the agent
// All items in group are a sequence of reasons and counts for this agent
foreach (var item in group)
{
// Item has <Agent, Reason, Count> and belongs to the agent from group.Key
}
}
Outer loop goes over all the agents (so Agent1, Agent2, etc.) while inner loop will go through all reasons for the current agent.

You might want to try GroupBy in LINQ :
You can read more about it here

Perhaps:
var agentGroups = finalResult
.GroupBy(x => x.Agent)
.Select(ag => new
{
Agent = ag.Key,
ReasonCounts = ag.GroupBy(x => x.Reason)
.Select(g => new
{
Agent = ag.Key,
Reason = g.Key,
Count = g.Sum(x => x.Count)
}).ToList(),
Total_Count = ag.Sum(x => x.Count)
});
foreach (var agentGroup in agentGroups)
{
string agent = agentGroup.Agent;
int totalCount = agentGroup.Total_Count;
foreach (var reasonCount in agentGroup.ReasonCounts)
{
string reason = reasonCount.Reason;
int count = reasonCount.Count;
}
}

Related

Get count of same value on table

I have this class where the query must result in this list a property.
This property must check on table how many duplicated exists.
This code works, but its very slow. can you help me ?
var lst = _uow.Repository.GetAll();
var query =
from p in lst
select new GetRfqResponse
{
ID = p.ID,
//bad performance
Count = lst.Where(x => x.Property == p.Property).AsQueryable().Count(),
//
};
Counting in a queryable list can be easily achieved using the Count() function:
// Find duplicated names
var byName = from s in studentList
group s by s.StudentName into g
select new { Name = g.Key, Count = g.Count() };
Check this fiddle to see it running.
Below is for InMemory
GroupBy should come to help.
var propertyGroupedList = list.GroupBy(l=>l.Property);
var query = list.Select(l => new GetRfqResponse{
Id = l.Id,
Count = propertyGroupedList.First(g=> g.Key == l.Property).Count()
});
Or you can create a dictionary with key as "Property" and value as count, then you will have to loop just once to store the count.
This allows you to get count in constant time
Dictionary<string, int> map = new Dictionary<string, int>();
foreach (var item in lst)
{
if (!map.ContainsKey(lst.Property))
{
map.Add(item.Property, 1);
}
else
map[item.Property]++;
}
var z = lst.Select(l => new GetRfqResponse{
Id = l.ID,
Count = map[l.Property]
});

foreach to linq expression help needed

having some trouble writing the following code to some nicer/less lines :)
any one have the good solution?
//custom implementation for popular filters
var popularFilter = new Dictionary<string, int>();
foreach (var car in allFilteredCars)
{
foreach (var offering in car.Offerings)
{
if (popularFilter.ContainsKey(offering))
popularFilter[offering] = popularFilter[offering] + 1;
else
popularFilter.Add(offering, 1);
}
}
categories.Add(new Category
{
Name = "popular",
Code = "popular",
Values = popularFilter.Select(p => new Value
{
Code = p.Key,
Name = p.Key,
Count = p.Value
}).ToList()
});
If it is possible i want i directly to add it in the categories list.
car.offerings = list<string>
so basicly something like:
Categories.Add(allFilteredCars.SelectMany(
c => c.Offerings.Select(
o => new {
something magical here}
.Select(a =>
new Category{
code.. etc etc..}
));
It looks like you just want to do a SelectMany to get the offerings, then group them and select the Count.
categories.Add(new Category
{
Name = "popular",
Code = "popular",
Values = allFilteredCars.SelectMany(c => c.Offerings)
.GroupBy(o => o)
.Select(grp => new Value
{
Code = grp.Key,
Name = grp.Key,
Count = grp.Count()
}).ToList()
});
Your non linq code already looks quite fine.
You can create your dictionary with linq by using a GroupBy & ToDictionary:
var dictionary = offerings
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());

How to filter a list based on 2 properties?

I have a list in my code that I need to filter through and return specific rows based on two criteria. The List in question is a list of models from a database. There are two ID properties on each model, one is the ID from the data table and is unique, the other is an ID we use to identify groups and can repeat. We'll call them ID and GroupID. Basically, I want the resulting list to have only one of each GroupID, and it should be the one with the highest (numerically speaking) ID. For example:
Input:
List<MyModel> modelList = new List<MyModel>
modelList[0].ID = 1 modelList[0].GroupID = 5
modelList[1].ID = 2 modelList[1].GroupID = 5
modelList[2].ID = 3 modelList[2].GroupID = 6
modelList[3].ID = 4 modelList[3].GroupID = 6
Desired Output:
Models at indexes 1 and 3.
Using LINQ:
var items = (from model in modelList
group model by model.GroupID into modelGroup
select modelGroup.Max(i => i.ID)).ToList();
What you have to do here is first order the modelList by ID and then GroupBy the list items by GroupID, then pull the item with max Id value.
var result = modelList.OrderByDescending(x => x.ID).GroupBy(x => x.GroupID).Select(x => x.First());
the above query will give you the result.
This is your solution:
var myData = models.GroupBy(model => model.GroupId)
.Select(group => group.OrderByDescending(model => model.Id).First());
Or you could also do this:
var myData = models.GroupBy(model => model.GroupId)
.Select(group => group.First(model => model.Id == group.Max(model1 => model1.Id)));
For fun, here's a fiddle.
You can try to use GroupBy.
var q = modelList.GroupBy(x => x.GroupID, x => x,
(key, g) => new {
GroupID = key,
Id = g.Max(c => c.ID)
});
This should group all your elements by GroupId and select Max ID in one of that groups.
Try this code:
List<MyModel> modelList = new List<MyModel>();
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList[0].ID = 1; modelList[0].GroupID = 5;
modelList[1].ID = 2; modelList[1].GroupID = 5;
modelList[2].ID = 3; modelList[2].GroupID = 6;
modelList[3].ID = 4; modelList[3].GroupID = 6;
var list = from ml in modelList group ml by ml.ID into r select new { ID = r.Key, MaxGroupID = r.Max() };
this might help you
modelList.GroupBy(model => model.GroupId, g => g.Id).Select(item => item.Max())
var newModelList = modelList.GroupBy(ml => ml.GroupID)
.Select(g => new MyModel
{
ID = g.OrderByDescending(x => x.ID).First().ID,
GroupID = g.Key
}).ToList();
Details
1) GroupBy then Select to get distinct items over GroupID.
2) First() after OrderByDescending to get highest ID.
3) new MyModel in Select is just to be explicit about the projection.

how to get the number of repetitions from List<int>

List<int> ListIdProducts = new List<int>();
var IdProductKey = from a in me.ProductKeywords where a.Keyword == item.Id select a;
foreach (var item2 in IdProductKey)
{
ListIdProducts.Add(item2.Product.Value);
}
Result is:
5
6
7
5
2
5
I need to get the following 5=3, 6=1, 7=1, 2=1
Use GroupBy LINQ method:
ListIdProducts
.GroupBy(i => i)
.Select(g => new { Value = g.Key, Count = g.Count() });
var query1 = from a in ListIdProducts
group a by new { a } into g
select new
{
item = g.Key,
itemcount = g.Count()
};
This a fairly standard group-by problem.
//untested
var IdProducts = from a in me.ProductKeywords
where a.Keyword == item.Id
group by a.Product.Value into g
select g.Count();

GroupBy with multiple groups as a hierarchy

I am using GroupBy create a hierarchical set of groups to use in multiple child grids.
Assume I have a query with with 6 columns, a, b, c, d, e, f.
Now, I need to group by a, then by b, then by c. and return the entire row in the group of c's.
var q = rows.GroupBy(x => x.a)
Ok, that's nice. That gives me my group of a's. Next, we look to group them by a and b.
var q1 = q.Select(g =>new {
Key = g.Key,
SubGroup = g.GroupBy(x => x.b)
}
Ok, that also works nice. I get my group of a's with subgroups of b's.
Now I'm stumped at the third level. I've tried various syntaxes, but most won't even compile. The ones that do do not give the correct results.
var q2 = q1.Select(g1 => new {
Key = g1.Key,
SubGroup = g1.GroupBy(x => x.c)
}
This doesn't compile. Tells me that there is no GroupBy on g1.
var q2 = q.Select(g1 => new {
Key = g1.Key,
SubGroup = g1.GroupBy(x => x.c)
}
This doesn't give me the b subgroup, only the a and c.
Any idea of what i'm doing wrong here?
EDIT:
The Following also does not work, saying there is no definition for the g1.Key
var q2 = q.Select(g => new {
Key = g.Key,
SubGroup = g.Select(g1 => new {
Key = g1.Key
SubGroup = g1.GroupBy(a => a.c)
})
I have such a poor grasp on what this is doing internally.
Now, I'm not saying this is actually a good approach; it's probably going to be slow and the right way to do this, if performance matters, may be to sort the whole collection by these different criteria and then look at the different parts of the sorted collection.
But if you want to use GroupBy and IGroupings to manage it, you're working at it from the wrong end. You want to start at the deepest level first and work up.
var groups = rows
.GroupBy(x => new { x.A, x.B, x.C, x.D, x.E, x.F })
.GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C, x.Key.D, x.Key.E })
.GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C, x.Key.D, })
.GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C })
.GroupBy(x => new { x.Key.A, x.Key.B })
.GroupBy(x => x.Key.A);
groups.First().Key; // will be an A value
groups.First().First().First(); // will be an A, B, C group
GroupBy actually supports giving a list of elements to group by. Each group will contain the same first 3 items (A, B & C). You can get the key with the .Key method, and play around with the different rows with foreach. See Example:
var groups = Elements.GroupBy(x => new {x.A, x.B, x.C});
foreach (var group in groups)
{
Trace.WriteLine(group.Key + ": " + group.Count());
foreach (var row in group)
{
Trace.WriteLine(row.D);
}
}
Edit: Ahh, ok - what you need is this then:
var groups = Elements
.GroupBy(a => new {a.A})
.Select(g1 => new {
A = g1.Key,
Groups = g1
.GroupBy(b=> new {b.B})
.Select(g2 => new {
B = g2.Key,
Groups = g2
.GroupBy(c => new {c.C})
.Select(g3 => new {
C = g3.Key,
Rows = g3
})
})
});
foreach (var groupA in groups)
{
Trace.WriteLine(groupA.A);
foreach (var groupB in groupA.Groups)
{
Trace.WriteLine("\t" + groupB.B);
foreach (var groupC in groupB.Groups)
{
Trace.WriteLine("\t\t" + groupC.C);
foreach (var row in groupC.Rows)
{
Trace.WriteLine("Row: " + row.ToString());
}
}
}
}

Categories

Resources