Today I ran into a case where I needed to convert a result set of IEnumerable<Entity> from a Linq2Sql command to a List<IEntity> in order to return the data back to the caller.
We all know that giving the following code
public class Entity : IEntity
We can do the following
IEntity myEntity = new Entity();
However, we cannot do this
IEnumerable<Entity> myEnumerationList = LoadFromLinq....
List<IEntity> myList = myEnumerationList;
The above syntax will cause a runtime exception complaining about invalid cast. In order to convert the list types we have to do it manually. Now there are at least 4 ways that I know of that will do this. I will describe then all here, and give you my take on which one is best.
Lets take a look at the actual code I had that required me to convert the IEnumeration to a List. (BTW, I am returning a List because I do NOT care about update/change notifications that I could get with a Collection).
private List GetInformation()
{
IEnumerable results = from sui in DBContextInstance().Table
select sui;
return results.ToList().ConvertAll(new Converter(ent => ent));
}
Method 1 - Works, but clunky
private List GetInformation()
{
IEnumerable results = from sui in DBContextInstance().Table
select sui;
List returnList = new List();
foreach (Entity myEntity in results)
{
returnList.Add(myEntity);
}
return returnList;
}
The above logic works, but is not that great. The biggest issue I have with this code is it is bloated and mucks up the method.
Method 2 - Better then 1, but still painful
private List GetInformation()
{
IEnumerable results = from sui in DBContextInstance().Table
select sui;
return results.ToList().ConvertAll(new Converter(Convert));
}
// method to do the casting/conversion
private IEntity Convert(Entity entity)
{
return entity;
}
This method does pretty much the same thing as method 1, but in place of having a loop, the ConvertAll method calls the newly created Convert method during each iteration.
Method 3 - Getting better
private List GetInformation()
{
IEnumerable results = from sui in DBContextInstance().Table
select sui;
return results.ToList().ConvertAll(new Converter(delegate(Entity entity) { return entity; }));
}
This method is nice because all the logic is on one line. This is the same as method 2, but in place of having to create a new method we are using a inline delegate to do the trick.
Method 4 - Lambda's rock
private List GetInformation()
{
IEnumerable results = from sui in DBContextInstance().Table
select sui;
return results.ToList().ConvertAll(new Converter(ent => ent));
}
Finally we are on to our shortest, and most 'elegant' example. This one uses a lambda expression in place of the inline delegate and archives the same result. The only draw back to this method is that if you are not familiar with Lambda's then this solution may be a little off putting.
Of the 4 different ways described above, I like Method 4. However method 3 will get you the exact same result and may be easier to follow for someone who does not know/understand Lambda expressions. If this is the case, then spend the 10 minutes and read up on Lambda's because they are pretty cool and very powerful.
Till next time,