Framework for Entities Choose a new POCO without. ToList() comes first.

entity-framework linq linq-to-entities linq-to-sql

Question

With a Silverlight 4 Client and a WCF Website service layer, I'm building an application. Since using RIA Services is not an option, we develop intermediary classes for communication. Assume for the sake of this query that I am exchanging tastyFood Objects.

public class FoodData
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Tastyness TastyLevel { get; set; }
}

The EF Model, a table with three fundamental fields, is effectively the same class (the Tastyness is an int that corresponds to our enum Tastyness).

When running Entity Framework queries, I notice that I often use the following kind of statement:

public List<FoodData> GetDeliciousFoods()
{
  var deliciousFoods = entities.Foods
                               .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                               .ToList()  // Necessary? And if so, best performance with List, Array, other?
                               .Select(dFood => dFood.ToFoodData())
                               .ToList();

  return deliciousFoods;
}

I receive an error without the.ToList() call saying that LINQ was unable to convert the custom function to a query equivalent, which I can understand.

The call to .ToList() with the custom extension before .Select(...) to convert our object to the POCO version of the Food object is the subject of my inquiry.

Is there a better pattern to follow in this situation, or perhaps a better option? Since I don't actually need the features of the List..> result, ToList() could be more efficient.

1
15
3/3/2011 8:37:33 PM

Accepted Answer

The issue with usingToList or AsEnumerable is that you actualize the whole thing and cover the fixup costs. The easiest way to ensure that SQL delivers just the necessary information is to project directly rather than utilizing.ToFoodData() :

var deliciousFoods = entities.Foods
                             .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = (Tastyness)dFood.Tastyness
                                  });

A potential issue is the cast to enum. If so, use an anonymous form:

var deliciousFoods = entities.Foods
                             .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = dFood.Tastyness
                                  })
                             .AsEnumerable()
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = (Tastyness)dFood.TastyLevel
                                  });

If you look at the resultant SQL, you'll find that it is much easier and that you save money by not having to make up objects for the ObjectContext.

12
3/3/2011 10:45:48 PM

Popular Answer

Use AsEnumerable() to remove the requirement for a redundant List and convert the query into a standard LINQ to Objects query.

var deliciousFoods = entities.Foods
                               .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                               .AsEnumerable()
                               .Select(dFood => dFood.ToFoodData())
                               .ToList();

Review: http://www.hookedonlinq.com/AsEnumerableOperator.ashx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow