Cannot implicitly convert type ‘System.Collections.Generic.List< >‘ to ‘System.Collections.Generic.IList< >‘

Assuming InvoiceMaster derives from or implements InvoiceHD, and that you’re using C# 4 and .NET 4 or higher, you can just use generic variance: This uses the fact that an IEnumerable<InvoiceMaster> is an IEnumerable<InvoiceHD> because IEnumerable<T> is covariant in T. Another way to solve it would be to change the declaration of MstDtl to use explicit typing: (I’d also suggest following regular C# naming, where local variables start with … Read more

What is the Java equivalent for LINQ?

There is nothing like LINQ for Java. … Edit Now with Java 8 we are introduced to the Stream API, this is a similar kind of thing when dealing with collections, but it is not quite the same as Linq. If it is an ORM you are looking for, like Entity Framework, then you can try Hibernate … Read more

Sequence contains no matching element

Well, I’d expect it’s this line that’s throwing the exception: First() will throw an exception if it can’t find any matching elements. Given that you’re testing for null immediately afterwards, it sounds like you want FirstOrDefault(), which returns the default value for the element type (which is null for reference types) if no matching items are found: … Read more

IEnumerable vs List – What to Use? How do they work?

IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away. Whenever I’m “stacking” LINQ expressions, I use IEnumerable, because by … Read more

Get single value from dictionary by key

It really does seem like you’re overcomplicating this issue. You can just use the indexer ([]) of the Dictionary class along with the .ContainsKey() method. If you use something like this: You should achieve the effect that you want.

Select distinct using linq

Edit: as getting this IEnumerable<> into a List<> seems to be a mystery to many people, you can simply write: But one is often better off working with the IEnumerable rather than IList as the Linq above is lazily evaluated: it doesn’t actually do all of the work until the enumerable is iterated. When you call ToList it actually walks the entire enumerable forcing all … Read more

Proper Linq where clauses

EDIT: LINQ to Objects doesn’t behave how I’d expected it to. You may well be interested in the blog post I’ve just written about this… They’re different in terms of what will be called – the first is equivalent to: wheras the latter is equivalent to: Now what difference that actually makes depends on the implementation of Where being called. If … Read more