linq-to-objects

Sequence contains no elements exception in linq without even using Single

偶尔善良 提交于 2019-12-19 05:01:26
问题 I am not using Single in LINQ below, but I am still getting a 'Sequence contains no elements' exception: allNames = StockCollection.Where((s) => s.Name.IndexOf("A") == 0) .Select((s) => s.Name) .Aggregate((namesInfo, name) => namesInfo += ", " + name); This exception comes when there is no stock starting with name 'A' . It seems that one extension method is expecting atleast one element satisfying the condition but that's not expected. Can you please suggest the best solution to resolve this?

With Entity Framework is it better to use .First() or .Take(1) for “TOP 1”?

主宰稳场 提交于 2019-12-18 18:35:52
问题 We are implementing some EF data repositories, and we have some queries which would include TOP 1 I have read many posts suggesting to use .Take(1) The code I'm reviewing uses .First() I understand that both of these produce the same result for the object assignment, but do they both actually resolve to the same query? When the DB is queried, will it actually be with TOP 1 for both requests? Or will they execute the query in full into the enumerable, then simply take the first entry in the

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

大憨熊 提交于 2019-12-18 13:48:18
问题 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. 回答1: 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>

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

时光毁灭记忆、已成空白 提交于 2019-12-18 13:13:07
问题 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

How can I filter a dictionary using LINQ and return it to a dictionary from the same type

无人久伴 提交于 2019-12-18 10:31:28
问题 I have the following dictionary: Dictionary<int,string> dic = new Dictionary<int,string>(); dic[1] = "A"; dic[2] = "B"; I want to filter the dictionary's items and reassign the result to the same variable: dic = dic.Where (p => p.Key == 1); How can I return the result as a dictionary from the same type [ <int,string> ] ? I tried ToDictionary , but it doesn't work. Thanks in advance. 回答1: ToDictionary is the way to go. It does work - you were just using it incorrectly, presumably. Try this:

Cannot serialize parameter of type 'System.Linq.Enumerable… ' when using WCF, LINQ, JSON

南笙酒味 提交于 2019-12-18 04:51:06
问题 I have a WCF Service. It uses Linq-to-objects to select from a Dictionary. The object type is simple: public class User { public Guid Id; public String Name; } There is a collection of stored in a Dictionary<Guid,User> . I want to have a WCF OperationContract method like this: public IEnumerable<Guid> GetAllUsers() { var selection = from user in list.Values select user.Id; return selection; } It compiles fine, but when I run it I get: The server encountered an error processing the request.

Why isn't Skip() in LINQ to objects optimized?

最后都变了- 提交于 2019-12-18 03:43:40
问题 var res = new int[1000000].Skip(999999).First(); It would be great if this query would just use the indexer instead of traversing 999999 entries. I had a look into the System.Core.dll and noticed that in contrast to Skip() , the Count() extension method is optimized. If the IEnumerable implements ICollection then it just calls the Count property. 回答1: If you look at my answer to a similar question, it appears as though it should be easy to provide a non-naive (i.e. throws proper exceptions)

LINQ return items in a List that matches any Names (string) in another list

泄露秘密 提交于 2019-12-17 17:37:12
问题 I have 2 lists. 1 is a collection of products. And the other is a collection of products in a shop. I need to be able to return all shopProducts if the names match any Names in the products. I have this but it doesn't seem to work. Any ideas? var products = shopProducts.Where(p => p.Name.Any(listOfProducts. Select(l => l.Name).ToList())).ToList(); I need to say give me all the shopproducts where name exists in the other list. 回答1: var products = shopProducts.Where(p => listOfProducts.Any(l =>

Parent/Children Xml to DTO Object Model with LINQ

…衆ロ難τιáo~ 提交于 2019-12-17 17:23:40
问题 Given the below DTO definitions: [Serializable] internal class OrderCollection : List<Order> { } [Serializable] internal class Order { public string OrderId { get; set; } public OrderDetailCollection OrderDetails { get; set; } } [Serializable] internal class OrderDetailCollection : List<OrderDetail> { } [Serializable] internal class OrderDetail { internal OrderDetail() { } /*public string ParentOrderId { get; set; }*/ public string ItemName { get; set; } public int Quantity { get; set; } }

linq to entities vs linq to objects - are they the same?

不打扰是莪最后的温柔 提交于 2019-12-17 15:34:47
问题 I usually use the term entity to represent a business data object and in my mind, the linq to entities and linq to objects were the same. Is that not correct? 回答1: That is definitely not the case. LINQ-to-Objects is a set of extension methods on IEnumerable<T> that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary. LINQ-to-Entities is a LINQ provider that has a set of extension methods on IQueryable<T> . The