linq-group

Dynamic Grouping Using LINQ

余生长醉 提交于 2019-12-08 01:12:44
问题 Please have a look at below example. The Group Clause has to be dynamic. Can you please guide me how this can be achieved. i.e. the row { r.Portfolio, r.DataType } has to be constructed dynamically. Not sure how I can tweak the solution as given at blog http://jonahacquah.blogspot.com/2012/02/groupby-multiple-columns-using-dynamic.html public class DecisionSupportData { public string Portfolio { get; set; } public string BucketName { get; set; } public string DataType { get; set; } public

Dynamic Grouping Using LINQ

牧云@^-^@ 提交于 2019-12-06 05:26:51
Please have a look at below example. The Group Clause has to be dynamic. Can you please guide me how this can be achieved. i.e. the row { r.Portfolio, r.DataType } has to be constructed dynamically. Not sure how I can tweak the solution as given at blog http://jonahacquah.blogspot.com/2012/02/groupby-multiple-columns-using-dynamic.html public class DecisionSupportData { public string Portfolio { get; set; } public string BucketName { get; set; } public string DataType { get; set; } public string ChildPortfolio { get; set; } } public void PopulateData() { List<DecisionSupportData> lstAllDecSupp

LINQ - GroupBy a key and then put each grouped item into separate 'buckets'

南楼画角 提交于 2019-12-04 00:03:48
I have a list of items as such: public class Item { public int ItemId { get; set; } public string ItemName { get; set; } public int ListId { get; set; } } 1 Test1 1 2 Test2 1 3 Test3 1 4 List 2 5 List2 2 6 Testing 3 7 Testing2 3 8 Testing3 3 Is there a way for me to group by the ListId and put them into each separate buckets, i.e, ListId1 bucket will have all items with ListId == 1 . The list is dynamically returned from SQL, so I don't know before hand how many ListId there will be. You can use GroupBy : var groups = items.GroupBy(item => item.ListId); foreach(var group in groups) { Console

How does GroupBy in LINQ work?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 09:27:54
问题 I originally have a dictionary of <string, List<ProviderSummary>> called rowsDictionary Now for each key of that dictionary I group its list of values by some criteria as below: Dictionary<string, List<ProviderSummary>> providerGroups = rowsDictionary.ToDictionary( x => x.Key, v => v.Value.GroupBy(x => new { x.GroupID, x.GroupFin, x.ZipCode }) .Select(x => x.First()) .ToList()); so for example if key["1234"] originally had 6 items in its list of values, now it may have two items based on that

List<string> Simple Group and Count?

纵饮孤独 提交于 2019-11-29 03:55:29
I have a very simple List<string> setup which contains lots of single characters per item (IE a foreach would console out to "a" "k" "p" etc) What I'd like to do is be able to group the items and also count how many of each occurs so I'd get an output similar to: a - 2 t - 3 y - 3 Any tips on the best way to do this? I am using .Net 4 if that's any help. (Given that each entry is a single character, is there any reason you don't have a List<char> by the way?) How about: // To get a Dictionary<string, int> var counts = list.GroupBy(x => x) .ToDictionary(g => g.Key, g => g.Count()); // To just

List<string> Simple Group and Count?

吃可爱长大的小学妹 提交于 2019-11-27 17:49:33
问题 I have a very simple List<string> setup which contains lots of single characters per item (IE a foreach would console out to "a" "k" "p" etc) What I'd like to do is be able to group the items and also count how many of each occurs so I'd get an output similar to: a - 2 t - 3 y - 3 Any tips on the best way to do this? I am using .Net 4 if that's any help. 回答1: (Given that each entry is a single character, is there any reason you don't have a List<char> by the way?) How about: // To get a

Select multiple fields group by and sum

帅比萌擦擦* 提交于 2019-11-26 20:48:43
问题 I want to do a query with linq (list of objects) and I really don't know how to do it, I can do the group and the sum but can't select rest of the fields. Example: ID Value Name Category 1 5 Name1 Category1 1 7 Name1 Category1 2 1 Name2 Category2 3 6 Name3 Category3 3 2 Name3 Category3 I want to group by ID, SUM by Value and return all fields like this. ID Value Name Category 1 12 Name1 Category1 2 1 Name2 Category2 3 8 Name3 Category3 回答1: Updated : If you're trying to avoid grouping for all

Use LINQ to concatenate multiple rows into single row (CSV property)

强颜欢笑 提交于 2019-11-26 20:43:53
I'm looking for the LINQ equivalent to the Sybase's LIST() or MySQL's group_concat() It'll convert: User Hobby -------------- Bob Football Bob Golf Bob Tennis Sue Sleeping Sue Drinking To: User Hobby -------------- Bob Football, Golf, Tennis Sue Sleeping, Drinking That's the GroupBy operator. Are you using LINQ to Objects? Here's an example: using System; using System.Collections.Generic; using System.Linq; public class Test { static void Main() { var users = new[] { new { User="Bob", Hobby="Football" }, new { User="Bob", Hobby="Golf" }, new { User="Bob", Hobby="Tennis" }, new { User="Sue",

Use LINQ to concatenate multiple rows into single row (CSV property)

痞子三分冷 提交于 2019-11-26 07:41:59
问题 I\'m looking for the LINQ equivalent to the Sybase\'s LIST() or MySQL\'s group_concat() It\'ll convert: User Hobby -------------- Bob Football Bob Golf Bob Tennis Sue Sleeping Sue Drinking To: User Hobby -------------- Bob Football, Golf, Tennis Sue Sleeping, Drinking 回答1: That's the GroupBy operator. Are you using LINQ to Objects? Here's an example: using System; using System.Collections.Generic; using System.Linq; public class Test { static void Main() { var users = new[] { new { User="Bob"