linq-to-objects

SqlException because Subquery returned more than 1 value

蓝咒 提交于 2019-12-02 18:21:28
问题 I have the following LINQ query that I am using to construct a structure to stuff into a JavaScript grid library which is irrelevant for this example, but I figured I would still explain that. var output = myObjects.Select( p => new RowModel { ID = p.LeadUID, Cells = new CellCollection(fields, p.myDataDatas.Where(q => q.myField.ParentUID == null).Select( q => new CellModel { Value = q.Value, Name = q.myField.Description, Display = q.myField.Description }).ToList() , new CellModel { Name =

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

老子叫甜甜 提交于 2019-12-02 17:24:11
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")) .Select(book => book.Title); titles.Dump(); In "LinqBooks.Common, Business Objects, Book.linq " is where the

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

只愿长相守 提交于 2019-12-02 17:01:54
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. You're trying to select multiple result objects for each ClassB object in the original list. Therefore, you're looking for the SelectMany extension method :

how to query LIST using linq

倖福魔咒の 提交于 2019-12-02 16:47:05
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. I would also suggest LinqPad as a convenient way to tackle with Linq for both advanced and beginners. Example: Well, the code you've given is invalid to start with - List is a generic type, and it has an

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

你离开我真会死。 提交于 2019-12-02 12:41:35
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 relational algebra. I have other logic which covers other criteria. Do not suggest reducing the logic shown

Extension method for UPDATE in Linq to Objects

依然范特西╮ 提交于 2019-12-02 10:31:48
问题 In the following scenario, I am querying a List object and for the matching predicate I want to update some values: var updatedList = MyList .Where (c => c.listItem1 != "someValue") .Update (m => {m.someProperty = false;}); The only issue is there is no Update extension method. How to go about this? My objective is update only those items in my list which match the condition leaving the other items intact. 回答1: using System; using System.Collections.Generic; using System.Linq; using System

Sorting an IEnumerable in LINQ

风格不统一 提交于 2019-12-01 20:10:26
问题 How to sort the given examples. IEnumerable<extra> eList = new List<extra>() { new extra{ id = 1, text = "a"}, new extra{ id = 2, text = "g"}, new extra{ id = 3, text = "i"}, new extra{ id = 4, text = "e"}, new extra{ id = 5, text = "f"}, new extra{ id = 6, text = "d"}, new extra{ id = 7, text = "c"}, new extra{ id = 8, text = "h"}, new extra{ id = 9, text = "b"} }; IEnumerable<sample> sam = new List<sample>() { new sample{ id = 1, name = "sample 1", list = new List<int>{1,5,6}}, new sample{

Performance difference between .where(…).Any() vs ..Any(…) [duplicate]

我只是一个虾纸丫 提交于 2019-12-01 18:36:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: LINQ extension methods - Any() vs. Where() vs. Exists() Given a list of objects in memory I ran the following two expressions: myList.where(x => x.Name == "bla").Any() vs myList.Any(x => x.Name == "bla") The latter was fastest always, I believe this is due to the Where enumerating all items. But this also happens when there's no matches. Im not sure of the exact WHY though. Are there any cases where this viewed

Performance difference between .where(…).Any() vs ..Any(…) [duplicate]

隐身守侯 提交于 2019-12-01 18:12:58
Possible Duplicate: LINQ extension methods - Any() vs. Where() vs. Exists() Given a list of objects in memory I ran the following two expressions: myList.where(x => x.Name == "bla").Any() vs myList.Any(x => x.Name == "bla") The latter was fastest always, I believe this is due to the Where enumerating all items. But this also happens when there's no matches. Im not sure of the exact WHY though. Are there any cases where this viewed performance difference wouldn't be the case, like if it was querying Nhib? Cheers. James Michael Hare The Any() with the predicate can perform its task without an

SortedSet / SortedList with better LINQ performance?

a 夏天 提交于 2019-12-01 17:29:35
Let's say we have a sorted collection such as SortedSet or SortedList with many (10M+) elements. Lots of querying is happening, so performance matters. From runtime comparisons, I'm under the impression that LINQ to Objects doesn't take advantage of the sorting, therefore not taking advantage of potential performance gains. First example - counting the elements in a range: var mySortedSet1 = new SortedSet<int>(); // populate ... int rangeCount = (from n in mySortedSet1 where ((n >= 1000000000) && (n <= 2000000000)) select n).Count(); Not exactly sure what LINQ to Objects does here internally,