linq-to-objects

How to merge a collection of collections in Linq

久未见 提交于 2019-12-03 10:26:27
问题 I would like to be able to fusion an IEnumerable<IEnumerable<T>> into IEnumerable<T> (i.e. merge all individual collections into one). The Union operators only applies to two collections. Any idea? 回答1: Try var it = GetTheNestedCase(); return it.SelectMany(x => x); SelectMany is a LINQ transformation which essentially says "For Each Item in a collection return the elements of a collection". It will turn one element into many (hence SelectMany). It's great for breaking down collections of

At least one object must implement IComparable

被刻印的时光 ゝ 提交于 2019-12-03 09:57:15
var listair = empcon.OrderBy(x => x.CustomerConnection.OrderBy(y => y.Id)).ToList(); When I am using this statement then I am getting exception "At least one object must implement IComparable" How can I solve this problem? I had this problem with my query when I wrote it wrong: IEnumerable<OrgRelation> relations = from r in tree.OrgRelations orderby r.ParentUnit, r.ChildUnit select r; This was because the Parent and Child Units are both OrgUnit objects the are related to this OrgRelation entity. What I needed was to order not by the object, but by the property of the object on which I really

LINQ to Objects and improved perf with an Index?

随声附和 提交于 2019-12-03 07:49:25
I am using LINQ to Objects and wonder if it is possible to improve the performance of my queries by making use of an index that I have. This is best explained with an example. Imagine a simple type... public class Person { public int Age; public string FirstName; public string LastName; } And a simple query I would make against it... List<Person> people = new List<Person>(); // 'people' populated with 50,000 instances... var x = from t in people where t.Age > 18 && t.Age < 21 select t; If I understand LINQ to Objects correctly then the implementation of the Where extension method will

Get a list of distinct items and their count

↘锁芯ラ 提交于 2019-12-03 05:37:19
I have an object, that has many properties but the only two to worry about are: myobject.ID which is an int myobject.Names which is a HashSet Then I have a List of those objects that looks something similar to this: List<myobject> I use Linq to get a some of the data into a repeater, but I'm not sure how to get the list of Names and how often they show up. Want to use Linq to avoid having to loop through the data. As my tags should show, this is an ASP.NET solution using C#. Some clarification: Lets say I have just three items in my list: Item 1 has John, Fred, Jack in its names. Item 2 has

How does LINQPad reference other classes, e.g. Books in the LINQ in Action samples

China☆狼群 提交于 2019-12-03 03:49:32
问题 I'm using LINQPad to create LINQ queries in an application I'm bulding. I noticed that in the downloaded LINQ in Action samples, e.g. example 4.04, intellisense shows a class "Books" but I don't see any references or " using " statements in the LINQPad tool, here is the sample: List<Book> books = new List<Book>() { new Book { Title="LINQ in Action" }, new Book { Title="LINQ for Fun" }, new Book { Title="Extreme LINQ" } }; var titles = books .Where(book => book.Title.Contains("Action"))

Using LINQ, select list of objects inside another list of objects

这一生的挚爱 提交于 2019-12-03 03:30:49
问题 public class ClassA { public string MyString {get; set;} } public class ClassB { public List<ClassA> MyObjects {get; set;} } List<ClassB> classBList = new List<ClassB>(); var results = (from i in classBList select i.MyObjects).Distinct(); I want a distinct list of all the ClassA objects in the classBList . How do I go about this using LINQ ? I'm thinking about a nested query, but couldn't quite figure it out. Any help is very appreciated. 回答1: You're trying to select multiple result objects

Filtering lists using LINQ

和自甴很熟 提交于 2019-12-03 02:36:27
I've got a list of People that are returned from an external app and I'm creating an exclusion list in my local app to give me the option of manually removing people from the list. I have a composite key which I have created that is common to both and I want to find an efficient way of removing people from my List using my List e.g class Person { prop string compositeKey { get; set; } } class Exclusions { prop string compositeKey { get; set; } } List<Person> people = GetFromDB; List<Exclusions> exclusions = GetFromOtherDB; List<Person> filteredResults = People - exclustions using the composite

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

馋奶兔 提交于 2019-12-02 22:13:41
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 nonManagers = from employee in employees where !(managers.Any(x => x.EmployeeId == employee.Id)) select

Implement “not in” (aka “not exists”) logic in LINQ

北战南征 提交于 2019-12-02 21:51:51
问题 Setup I have two List<T> 's. The data is un-normalized and from different sources which explains the convolution in the desired logic An informal compound key in the data is fieldA, fieldB, fieldC. The "fields" are strings - reference types - so their values could be null. I want to drop records where they may be matching on null. I get that null references in C# will match, but in SQL they do not. Adding a !string.IsNullOrEmpty() is easy enough. This is not a question about DB design or

Optimizing Aggregate for String Concatenation [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-02 20:37:35
Closed . This question needs details or clarity. It is not currently accepting answers. Learn more . Want to improve this question? Add details and clarify the problem by editing this post . 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 System.Linq; using System.Diagnostics; namespace ConsoleApplication5 { class Program { static void Main(string[