linq-to-objects

IList<T> to IQueryable<T>

混江龙づ霸主 提交于 2019-11-26 14:10:06
问题 I have an List and I'd like to wrap it into an IQueryable. Is this possible? 回答1: List<int> list = new List<int>() { 1, 2, 3, 4, }; IQueryable<int> query = list.AsQueryable(); If you don't see the AsQueryable() method, add a using statement for System.Linq . 回答2: Use the AsQueryable<T>() extension method. 来源: https://stackoverflow.com/questions/73542/ilistt-to-iqueryablet

“Nested foreach” vs “lambda/linq query” performance(LINQ-to-Objects) [closed]

一笑奈何 提交于 2019-11-26 12:42:37
问题 In performance point of view what should you use \"Nested foreach\'s\" or \"lambda/linq queries\"? 回答1: Write the clearest code you can, and then benchmark and profile to discover any performance problems. If you do have performance problems, you can experiment with different code to work out whether it's faster or not (measuring all the time with as realistic data as possible) and then make a judgement call as to whether the improvement in performance is worth the readability hit. A direct

Find() vs. Where().FirstOrDefault()

三世轮回 提交于 2019-11-26 12:02:30
问题 I often see people using Where.FirstOrDefault() to do a search and grab the first element. Why not just use Find() ? Is there an advantage to the other? I couldn\'t tell a difference. namespace LinqFindVsWhere { class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.AddRange(new string[] { \"item1\", \"item2\", \"item3\", \"item4\" }); string item2 = list.Find(x => x == \"item2\"); Console.WriteLine(item2 == null ? \"not found\" : \"found\"); string

Sorting a list using Lambda/Linq to objects

独自空忆成欢 提交于 2019-11-26 12:02:02
I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string LastName {set; get;} public DateTime DOB {set; get;} } public void Sort(ref List<Employee> list, string sortBy, string sortDirection) { //Example data: //sortBy = "FirstName" //sortDirection = "ASC" or "DESC" if (sortBy == "FirstName") { list = list.OrderBy(x => x.FirstName).toList(); } } Instead of using a bunch of ifs to check the fieldname (sortBy), is there a cleaner way of doing the sorting Is sort

Linq select objects in list where exists IN (A,B,C)

只谈情不闲聊 提交于 2019-11-26 08:01:50
问题 I have a list of orders . I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in (\"A\", \"B\", \"C\") // Filter the orders based on the order status var filteredOrders = from order in orders.Order where order.StatusCode.????????(\"A\", \"B\", \"C\") select order; 回答1: Your status-codes are also a collection, so use Contains: var allowedStatus = new[]{ "A", "B", "C" }; var filteredOrders = orders.Order.Where(o => allowedStatus.Contains

How can I get LINQ to return the object which has the max value for a given property? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-26 07:58:20
问题 This question already has answers here : How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate] (9 answers) How to use LINQ to select object with minimum or maximum property value (12 answers) Closed 4 years ago . If I have a class that looks like: public class Item { public int ClientID { get; set; } public int ID { get; set; } } And a collection of those items... List<Item> items = getItems(); How can I use LINQ to return the

How to create a dynamic LINQ join extension method

▼魔方 西西 提交于 2019-11-26 07:26:47
问题 There was a library of dynamic LINQ extensions methods released as a sample with Visual Studio 2008. I\'d like to extend it with a join method. The code below fails with a parameter miss match exception at run time. Where is the problem? public static IQueryable Join(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values) { if (inner == null) throw new ArgumentNullException(\"inner\"); if (outerSelector == null)

LINQ to SQL and a running total on ordered results

断了今生、忘了曾经 提交于 2019-11-26 06:06:25
问题 I want to display a customer\'s accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, looping through the data, and adding rows to the DataGridView one-by-one and calculating the running total at that time. Lame. I would much rather use LINQ to SQL, or LINQ if not possible with LINQ to SQL, to figure out the running totals so I can just set DataGridView.DataSource to my data. This is

Select distinct by two properties in a list

半城伤御伤魂 提交于 2019-11-26 05:35:19
问题 I have a list<message> that contains properties of type Guid and DateTime (as well as other properties). I would like to get rid of all of the items in that list where the Guid and DateTime are the same (except one). There will be times when those two properties will be the same as other items in the list, but the other properties will be different, so I can\'t just use .Distinct() List<Message> messages = GetList(); //The list now contains many objects, it is ordered by the DateTime property

Learning about LINQ [closed]

守給你的承諾、 提交于 2019-11-26 04:57:09
问题 Overview One of the things I\'ve asked a lot about on this site is LINQ. The questions I\'ve asked have been wide and varied and often don\'t have much context behind them. So in an attempt to consolidate the knowledge I\'ve acquired on Linq I\'m posting this question with a view to maintaining and updating it with additional information as I continue to learn about LINQ. I also hope that it will prove to be a useful resource for other people wanting to learn about LINQ. What is LINQ? From