linq-to-objects

Order by fields in an anonymous type

*爱你&永不变心* 提交于 2019-12-11 10:26:29
问题 I am using LINQ to Objects to aggregate: var summary = from esc in objs where esc.time.Month == month && esc.time.Year == year group esc by esc.rlf_id into g select new { ID = g.Key, Total = g.Count(), Preventable = g.Where(a => a.preventable).Count() }; My query works as I would expect, but I also want to order the query by arbitrary field(s) in the anonymous type. I found LINQ: Order By Anonymous Type, but it's in VB.NET and requires strongly specifying what field to sort by. I can

Lambda Expression to get the cells vertically for a section containing rows

大兔子大兔子 提交于 2019-12-11 09:35:04
问题 I have to iterate through each section and create the cells for rows to ultimately create a table. Here each Section has rows 0....n , and Rows have cells say 0...n For example as illustrated below: Row 0-> Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5 Row 1-> Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5 Row 2-> Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5 .. I want to create a lambda expression (say): var allColumns = ........... To get all Cell0's then all Cell1's, all Cell2's etc... so that

LINQ - Combine multiple lists to form a new list and align them by key?

扶醉桌前 提交于 2019-12-11 04:08:02
问题 I have two list of different columns, but each list have a common column with the same key, how do I combine them into a new list, i.e: public class TradeBalanceBreak { public int CommID { get; set; } public int CPFirmID { get; set; } public double CreditDfferenceNotional { get; set; } public string Currency { get; set; } } public class Commission { public int CommID { get; set; } public PeriodStart { get; set; } public ResearchCredit { get; set; } } public class CommissionList { public List

aggregate list with linq with sum

最后都变了- 提交于 2019-12-11 02:41:26
问题 i am trying to consolidate an ienumerable list that i am serializing i have data that looks like this: Internet explorer 10 Internet explorer 15 Mozille firefox 10 I was it to look like: Internet explorer 25 Mozille firefox 10 my class looks like: public class BrowserVisits { public string BrowserName { get; set; } public int Visits { get; set; } } my current query to serialize an ienumerable list (r) looks like: var browserVisits = from r in reportData select new BrowserVisits { BrowserName

LINQ Left Outer JOIN Syntax Difference

旧时模样 提交于 2019-12-11 02:41:04
问题 There are at least two ways of doing LEFT OUTER JOIN in LINQ class Customer { public int ID { get; set; } public string Name { get; set; } } class Order { public int ID { get; set; } public string Product { get; set; } } static void Main() { // Example customers. var customers = new Customer[] { new Customer{ID = 5, Name = "Sam"}, new Customer{ID = 6, Name = "Dave"}, new Customer{ID = 7, Name = "Julia"}, new Customer{ID = 8, Name = "Sue"}, new Customer{ID = 9, Name = "Joe"} }; // Example

Efficient way to remove items from dictionary

放肆的年华 提交于 2019-12-11 00:23:37
问题 I have two dictionaries dict1 and dict2, I would like to remove items from dict1 which are present in dict2 using its key. Instead of looping through dict2 and using "ContainsKey" method, ia there any other approach like using linq. 回答1: The appropriate way to do this would be: foreach(var key in dic2.Keys) { dic1.Remove(key); } LINQ stands for Language Integrated Query . It is for performing queries on data. Queries do not mutate the underlying structures. Since what you want to do is to

How to convert a String[] to an IDictionary<String, String>?

一个人想着一个人 提交于 2019-12-10 21:25:56
问题 How to convert a String[] to an IDictionary<String, String> ? The values at the indices 0,2,4,... shall be keys, and consequently values at the indices 1,3,5,... shall be values. Example: new[] { "^BI", "connectORCL", "^CR", "connectCR" } => new Dictionary<String, String> {{"^BI", "connectORCL"}, {"^CR", "connectCR"}}; 回答1: Dictionary<string,string> ArrayToDict(string[] arr) { if(arr.Length%2!=0) throw new ArgumentException("Array doesn't contain an even number of entries"); Dictionary<string

Comparing i4o vs. PLINQ for larger collections

北战南征 提交于 2019-12-10 19:30:54
问题 I have a question for anyone who has experience on i4o or PLINQ. I have a big object collection (about 400K ) needed to query. The logic is very simple and straightforward. For example, there has a collection of Person objects, I need to find the persons matched with same firstName, lastName, datebirth, or the first initial of FirstName/lastname, etc. It is just a time consuming process using LINQ to Object. I am wondering if i4o (http://www.codeplex.com/i4o) or PLINQ can help on improving

Left Join LINQ and using Bitwise Comparisons

橙三吉。 提交于 2019-12-10 19:27:06
问题 Left Join LINQ and using Bitwise Comparisons. I have a problem that can be described as (thanks to David B for clarifying this): The goal is to return 1 row per OID from the Left Table, where the Count of the records in the left table is equal to the Count of the matching rows in the right table. A record matches when the OID, RID, and the FLAG is set in FLAGS for a row. The Objects we are comparing have the following structure: public class Roads : List<Road>{} public class Road { public int

How to select multiple values after using Max() in LINQ to Objects?

给你一囗甜甜゛ 提交于 2019-12-10 19:08:01
问题 I have the following LINQ query: var query = (from p in obj1 group p by p.objID into g let totalSum = g.Sum(p => p.ObjPrice) select new { MyObjectID = g.Key, totalSum }) .Max(g => g.totalSum); I want to select both the object id and price of the object with the maximum price. How can I do that? 回答1: Use an order by descending clause and call FirstOrDefault() . (from p in obj1 group p by p.objID into g let totalSum = g.Sum(p => p.ObjPrice) orderby totalSum descending select new { MyObjectID =