Filtering lists using LINQ

Have a look at the Except method, which you use like this: var resultingList = listOfOriginalItems.Except(listOfItemsToLeaveOut, equalityComparer) You’ll want to use the overload I’ve linked to, which lets you specify a custom IEqualityComparer. That way you can specify how items match based on your composite key. (If you’ve already overridden Equals, though, you shouldn’t need … Read more

Linq: List of lists to a long list

ybo’s answer would have been my first response too. The query expression equivalent of this is: var query = from a in computeAList() from b in a.Alist select b.C; For the sake of completeness, the other answers in this thread are variations on the same theme. From ybo (the exact same query, expressed as dot … Read more

NHibernate 3.0: No FirstOrDefault() with QueryOver?

I have now found out that I could use the Take() extension method on the IQueryOver instance, and only the enumerate to a list, like so: Result precedingOrMatchingResult = Session.QueryOver<Result>(). Where(r => r.TimeStamp < timeStamp). OrderBy(r => r.TimeStamp).Desc. Take(1).List(). //enumerate only on element of the sequence! FirstOrDefault(); //get the preceding or matching result, if there … Read more

Generate EF orderby expression by string [duplicate]

Using reflection and expression-trees you can provide the parameters and then call OrderBy function, Instead of returning Expression<Func<Task, T>> and then calling OrderBy. Note that OrderBy is an extension method and has implemented in both System.Linq.Enumarable and System.Linq.Queryable classes. The first one is for linq-to-objects and the latter is for linq-to-entities. entity-framework needs the expression … Read more

Find the most occurring number in a List

How about: var most = list.GroupBy(i=>i).OrderByDescending(grp=>grp.Count()) .Select(grp=>grp.Key).First(); or in query syntax: var most = (from i in list group i by i into grp orderby grp.Count() descending select grp.Key).First(); Of course, if you will use this repeatedly, you could add an extension method: public static T MostCommon<T>(this IEnumerable<T> list) { return … // previous code … Read more