linq-group

Why do changes made in foreach to a Linq grouping select get ignored unless I add ToList()?

送分小仙女□ 提交于 2021-02-05 05:29:45
问题 I have the following method. public IEnumerable<Item> ChangeValueIEnumerable() { var items = new List<Item>(){ new Item("Item1", 1), new Item("Item2", 1), new Item("Item3", 2), new Item("Item4", 2), new Item("Item5", 3) }; var groupedItems = items.GroupBy(i => i.Value) .Select(x => new Item(x.First().Name, x.Key)); foreach (var item in groupedItems) { item.CalculatedValue = item.Name + item.Value; } return groupedItems; } Into the groupedItems collection the CalculatedValue s are null.

Access all of the data after joining two tables and group them using linq

偶尔善良 提交于 2020-01-12 14:15:09
问题 I have two tables TableA aId aValue TableB bId aId bValue I want to join these two tables via aId , and from there, group them by bValue var result = from a in db.TableA join b in db.TableB on a.aId equals b.aId group b by b.bValue into x select new {x}; My code doesn't recognize the join after the group. In other words, the grouping works, but the join doesn't (or at least I can't figure out how to access all of the data after the join). 回答1: The expression between the group and the by

Group by array contents

若如初见. 提交于 2019-12-24 12:12:06
问题 I have a List<Tuple<string,long,byte[]>> and I want to group by the contents of the byte array. Is there a simple way to do this with GroupBy and a lambda? Ideally, I want to do this without creating an intermediate data structure (like a string to hold the elements of the array). 回答1: You can achieve that using custom IEqualityComparer<byte[]> (or even better, generic one: IEqualityComparer<T[]> ) implementation: class ArrayComparer<T> : IEqualityComparer<T[]> { public bool Equals(T[] x, T[]

Using LINQ Group Joins in VB.NET

孤者浪人 提交于 2019-12-23 15:18:04
问题 I'm trying to figure out how to use Group Joins in LINQ queries under VB.NET. For some reason, every example I seem to find on the syntax is just plain WRONG! At least, that's what my compiler keeps telling me. What is it exactly I'm doing wrong here? This is a simple example where I want to join orders to their order items so that I end up with a type that contains a collection of order items grouped together by their orderId's: Dim groupedOrders = (From o In orders Group Join i In

DataTable group the result in one row

◇◆丶佛笑我妖孽 提交于 2019-12-14 02:53:12
问题 I have a DataTable and want to group Name, LastName and Comment. The rest should be in the same row. In my Code firstly i make ID's values as header and then organize the Attribute values to each ID. What I want here is to group the the same Name, Lastname and Comment with their ID values. My first Table looks like that: ID Name Lastmame Comment Attribute 1 kiki ha hello FF 3 lola mi hi AA 2 ka xe what UU 2 kiki ha hello SS After I use my code: Name Lastname Comment 1 3 2 kiki ha hello FF

Select duplicates from multiple lists

橙三吉。 提交于 2019-12-13 04:38:18
问题 I have an array of List<int> , I'm using LINQ (thanks to this forum), to find duplicates, but after merging lists into one list, how can I retrieve a dictionary like this : KEY -> duplicate value | VALUE -> list index where duplicate was found Actually I'm doing this : List<int> duplicates = hits.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); Guess I should use SelectMany 回答1: You can map every element to (item, index) and then it will be easy to selected impacted

Per Invoice show string/int array of unique products

℡╲_俬逩灬. 提交于 2019-12-12 03:59:57
问题 I have a list of Invoices and all the Products on each Invoice. Each Invoice can have multiples of the same product class InvoiceProducts { public int InvoiceID { get; set; } public int ProductID { get; set; } } var list = new List<InvoiceProducts>(); list.Add(new { InvoiceID = 7000, ProductID=15}); list.Add(new { InvoiceID = 7000, ProductID=10}); list.Add(new { InvoiceID = 7000, ProductID=10}); list.Add(new { InvoiceID = 7000, ProductID=15}); list.Add(new { InvoiceID = 7010, ProductID=12});

DataTable group the result

瘦欲@ 提交于 2019-12-11 14:28:46
问题 I want to group in Datatable by Name, LastName and the rest should be in same row. Can someone help me with it? My DataTable: Name LastName 1 3 2 kiki ha FF lola mi AA ka xe UU kiki ha SS I want to have DataTable group by Name: Name LastName 1 3 2 kiki ha FF SS lola mi AA ka xe UU My new code: var result11 = from t1 in newtable.AsEnumerable() group t1 by new { Name = t1.Field<String>("Name"), LastName = t1.Field<String>("LastName") } into grp select new { Name = grp.Key.Name, LastName = grp

LINQ Grouping by custom type not working

天涯浪子 提交于 2019-12-10 14:57:31
问题 I have data I receive from a web service via HTTPWebRequest . After I parse it using NewtonSoft.Deserialize into a custom type (a simple class with public string properties), I want to manipulate this data using LINQ - more specifically, I want to group the data. My problem is that the grouping works fine if I group by a single string property from x in myList group x by x.myStr into grp select grp; Since I want to group by more columns, I am returning a custom type with new MyType { a = ...,

Tricky Linq Group by for time ranges

↘锁芯ラ 提交于 2019-12-08 05:45:11
问题 I have a class that represents a shift that employee's can work: public class Shift { public int Id { get; set;} public DateTime Start {get;set;} public DateTime End { get; set;} public DayOfWeek Day { get; set;} } And say I have a list of these shifts for a single employee: List<Shift> myShifts; I know I can get group the shifts by day with the following linq statement: var shiftsByDay = from a in myShift group a by a.Day; My question: For each day, how can I get all the shifts that overlap,