linq-to-objects

OrderBy and Top in LINQ with good performance

梦想的初衷 提交于 2019-12-20 12:11:13
问题 What is a good way to get the top 10 records from a very large collection and use a custom OrderBy? If I use the LINQ to Objects OrderBy method it is slow and takes a lot of memory because it creates an entire new collection with the new order. I would like a new method with the signature below that does not re-order the entire collection and is very fast: public static IEnumerable<TSource> OrderByTop<TSource, TKey>( IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey

Using LINQ to Objects to find items in one collection that do not match another

假如想象 提交于 2019-12-20 10:24:34
问题 I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. A LINQPad example of what I'm trying to do: void Main() { var employees = new[] { new Employee { Id = 20, Name = "Bob" }, new Employee { Id = 10, Name = "Bill" }, new Employee { Id = 30, Name = "Frank" } }; var managers = new[] { new Manager { EmployeeId = 20 }, new Manager { EmployeeId = 30 } }; var

Optimizing Aggregate for String Concatenation [closed]

余生颓废 提交于 2019-12-20 09:54:05
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Update - for those of a facetious frame of mind, you can assume that Aggregate still produces the normal result whatever function is passed to it, including in the case being optimized. I wrote this program to build a long string of integers from 0 to 19999 separate by commas. using System; using

Using LINQ to group a list of objects

感情迁移 提交于 2019-12-20 09:35:14
问题 I have an object: public class Customer { public int ID { get; set; } public string Name { get; set; } public int GroupID { get; set; } } I return a list that may look like the following: List<Customer> CustomerList = new List<Customer>(); CustomerList.Add( new Customer { ID = 1, Name = "One", GroupID = 1 } ); CustomerList.Add( new Customer { ID = 2, Name = "Two", GroupID = 1 } ); CustomerList.Add( new Customer { ID = 3, Name = "Three", GroupID = 2 } ); CustomerList.Add( new Customer { ID = 4

how to query LIST using linq

风格不统一 提交于 2019-12-20 08:43:41
问题 suppose if i add person class instance to list and then i need to query the list using linq. List lst=new List(); lst.add(new person{ID=1,Name="jhon",salaty=2500}); lst.add(new person{ID=2,Name="Sena",salaty=1500}); lst.add(new person{ID=3,Name="Max",salaty=5500}); lst.add(new person{ID=4,Name="Gen",salaty=3500}); now i want to query the above list with linq. please guide me with sample code. 回答1: I would also suggest LinqPad as a convenient way to tackle with Linq for both advanced and

LINQ and CASE Sensitivity

旧街凉风 提交于 2019-12-20 03:38:12
问题 I have this LINQ Query: TempRecordList = new ArrayList(TempRecordList.Cast<string>().OrderBy(s => s.Substring(9, 30)).ToArray()); It works great and performs sorting in a way that's accurate but a little different from what I want. Among the the result of the query I see something like this: Palm-Bouter, Peter Palmer-Johnson, Sean Whereas what I really need is to have names sorted like this: Palmer-Johnson, Sean Palm-Bouter, Peter Basically I want the '-' character to be treated as being

Case insensitive string compare with linq-to-sql and linq-to-objects

这一生的挚爱 提交于 2019-12-19 16:50:23
问题 see also Differences between LINQ to Objects and LINQ to SQL queries We are using the some queries over our database and our in memory objects . What is the best way of doing an insensitive string compare with linq-to-sql so that? It runs fast on SQL Server The same query expression can be used with linq-to-objects to get the same result Using a.ToLowerInvariant() == b.ToLowerInvariant() at least gets the same results, but it does not get processed on SQL server as far as I can tell, so can

How linq 2 sql is translated to TSQL

拟墨画扇 提交于 2019-12-19 11:43:46
问题 It seems Linq2sql doesn't know how to construct the TSQL while you transform linq2sql object into domain object with constructors. Such as: from c in db.Companies select new Company (c.ID, c.Name, c.Location).Where(x => x.Name =="Roy"); But when using settable attributes, it will be OK. from c in db.Companies select new Company { ID = c.ID, Name = c.Name, Location = c.Location }.Where(x => x.Name =="Roy"); I don't want to allow those attributes to be settable. How can I achieve this? And can

Linq dynamic queries for user search screens

两盒软妹~` 提交于 2019-12-19 10:24:40
问题 I have a database that has a user search screen that is "dynamic" in that I can add additional search criteria on the fly based on what columns are available in the particular view the search is based on and it will allow the user to use them immediately. Previously I had been using nettiers for this database, but now I am programming a new application against it using RIA and EntFramework 4 and LINQ. I currently have 2 tables that are used for this, one that fills the combobox with the

Linq Lambda vs Query Syntax Performance

谁说我不能喝 提交于 2019-12-19 05:22:49
问题 i saw today a linq query syntax in my project which was counting from List items on specifc condition this way: int temp = (from A in pTasks where A.StatusID == (int)BusinessRule.TaskStatus.Pending select A).ToList().Count(); i thought to refactor it by writing it like using Count() to make more readable and what i thought was it would be performance wise also good, so i wrote: int UnassignedCount = pTasks.Count(x => x.StatusID == (int)BusinessRule.TaskStatus.Pending); But when i checked by