linq-to-objects

How to sort a List<T> by double value?

百般思念 提交于 2019-11-30 10:58:59
This sound simple but it not that much. I want to order a List based on one of the properties of T, which is double type. If you know the propertyname before compilation: myList = myList.OrderBy(a=>a.propertyName).ToList(); or myList = (from m in myList order by m.propertyName).ToList(); If you don't have the property at compile time (e.g. dynamic sorting in a grid or something); try the following extension methods: static class OrderByExtender { public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> collection, string key, string direction) { LambdaExpression sortLambda =

Share expressions between Linq to Entities and Linq to Objects

心已入冬 提交于 2019-11-30 09:09:48
问题 I'm trying to "share" a set of conditions between a Linq to Entities call and a some other code, to reduce possible mismatches in conditions between the two calls. I started off by declaring my conditions: private Func<DateTime, Status, bool> _submissionDateExpiredCondition = (submissionDate, status) => submissionDate < DateTime.Now && status == Status.OK; private Func<DateTime, Status, bool> _submissionDateWithinOneWeekCondition = (submissionDate, status) => DateTime.Now < DbFunctions

Why does using anonymous type work and using an explicit type not in a GroupBy?

孤街浪徒 提交于 2019-11-30 09:06:16
I have a problem where I want a group type to be strongly typed but if I do it doesn't group correctly. See the code below... using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication35 { class Program { static void Main(string[] args) { List<Foo> foos = new List<Foo>(); foos.Add(new Foo() { Key = "Test" }); foos.Add(new Foo() { Key = "Test" }); foos.Add(new Foo() { Key = "Test" }); var groups = foos.GroupBy<Foo, dynamic>(entry => new { GroupKey = entry.Key }); Console.WriteLine(groups.Count()); groups = foos.GroupBy<Foo, dynamic>(entry => new GroupingKey(

Whats the 'modern' way to find common items in two Lists<T> of objects?

丶灬走出姿态 提交于 2019-11-30 09:04:16
I have two Generic Lists containing different types, for the sake of example, lets call them Products and Employees . I'm trying to find Products that are based at the same location as Employees, i.e. where product.SiteId == emp.SiteId List<Product> lstProds; List<Employees> lstEmps; My (old skool) brain is telling me to use a forEach loop to find the matches but I suspect there is a ('better'/terser/faster?) way to do it using Linq. Can anyone illuminate me? All the examples I've found online deal with Lists of primitives (strings/ints) and are not especially helpful. I would say: var

C# Merging 2 dictionaries

家住魔仙堡 提交于 2019-11-30 08:09:23
I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default settings and the 2nd dictionary contains some user defined settings. var default_settings = new Dictionary<string, MyElementSettings>(); var custom_settings = new Dictionary<string, MyElementSettings>(); I would like to combine the 2 dictionaries into one that contains the elements of both dictionaries. The problem that I am running into is it is possible

LINQ - is SkipWhile broken?

北城余情 提交于 2019-11-30 07:59:19
I'm a bit surprised to find the results of the following code, where I simply want to remove all 3s from a sequence of ints: var sequence = new [] { 1, 1, 2, 3 }; var result = sequence.SkipWhile(i => i == 3); // Oh noes! Returns { 1, 1, 2, 3 } Why isn't 3 skipped? My next thought was, OK, the Except operator will do the trick: var sequence = new [] { 1, 1, 2, 3 }; var result = sequence.Except(i => i == 3); // Oh noes! Returns { 1, 2 } In summary, Except removes the 3, but also removes non-distinct elements. Grr. SkipWhile doesn't skip the last element, even if it matches the condition. Grr.

DataTable Select vs LINQ Select

做~自己de王妃 提交于 2019-11-30 06:58:43
Any advice on when DataTable.Select should be used versus LINQ Select when dealing with an in-memory DataTable? I find LINQ syntax easier and more powerful, but I'm not sure if there are performance or other issues which make a DataTable select preferable. (I'm using a third party API that provides a DataTable that has been pre-populated from the database. I need to filter that further in-memory.) Based upon personal experience, I try to avoid the Datatable.Select. I find it to be slow and has some odd bugs. One (confirmed and documented by Microsoft) bug I ran into was that DataTable.Select

LINQ to JSON: InvalidOperationException: Lambda Parameter not in scope

本小妞迷上赌 提交于 2019-11-30 05:31:13
问题 I am trying the execute the sample code of LINQ to JSON (mentioned below) but it is giving me following error Stack Trace: [InvalidOperationException: Lambda Parameter not in scope] Code I am executing is: JObject rss = new JObject( new JProperty("id", "James Newton-King"), new JProperty("name", "http://james.newtonking.com"), new JProperty("data", "James Newton-King's blog."), new JProperty("children", new JArray( from p in mwsysbot.Software where p.SoftwareName == name select new JObject(

How to use the IN operator in linq

╄→гoц情女王★ 提交于 2019-11-30 03:31:06
问题 I'm querying a view and filtering the results with a column named status. I'd like to query it so I can search for rows with different status, by using the IN operator as I'd do in SQL. As so: SELECT * FROM VIEW WHERE Status in ('....', '.....') How can I achieve this? 回答1: If your query expression uses the Contains method of an IEnumerable object, the parser will turn that into an IN expression using the values in the IEnumerable . List<string> foo = new List<string>() { "a", "b", "c" }; var

Feed a LINQ result into a DataROW

岁酱吖の 提交于 2019-11-29 22:52:54
问题 This works: var Result = from e in actual.Elements select new { Key = e.Key, ValueNumber = e.Value.ValueNumber, ValueString = e.Value.ValueString, ValueBinary = e.Value.ValueBinary, ValueDateTime = e.Value.ValueDateTime }; But this doesn't work: IEnumerable<DataRow> Result = from e in actual.Elements select new DataRow { Key = e.Key, ValueNumber = e.Value.ValueNumber, ValueString = e.Value.ValueString, ValueBinary = e.Value.ValueBinary, ValueDateTime = e.Value.ValueDateTime }; DataTable dt =