1

Using Entity Framework, is IEnumerable the correct container to use to send back a generic data set? I.e. when I do not want to send back a list of the object, but just a generic a result set.

public IEnumerable<object> SelectPlayerFirstAndLastNameList()
{
   return (from p in rlpEntities.Players select new { p.PlayerFirstName, p.PlayerLastName });
}

Thanks.

3 Answers3

0

Check this link out Why use ICollection and not IEnumerable or List<T> on many-many/one-many relationships?.

It really depends on your scenario, but IEnumerable<> would be used when you need to iterate, and List<> when you need to iterate and modify or sort the data.

IEnunerable<> - http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx List<> - http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx You can also use generics, to pass on whatever types you are querying against, like for instance

public IEnumerable<T> SelectPlayerFirstAndLastNameList<T>()
{
    return (IEnumerable<T>)(from p in rlpEntities.Players);
}

So you can pass either object, or a known defined type. To call this you would do

var x = SelectPlayerFirstAndLastNameList<YourClassHere>();
Community
  • 1
  • 1
Mez
  • 4,666
  • 4
  • 29
  • 57
  • My question isn't so much about IEnumerable vs List but rather vs something else. thanks – user3267781 Apr 22 '14 at 14:12
  • Updated answer to use generics @user3267781 – Mez Apr 22 '14 at 14:16
  • Your answer with generics makes no sense, with the question he is asking. Have a closer look to his method, he is returning an anonymous query with `select new`, to make your generic approach work it had to be `select new T { }`, which than would need the `new()` keyword for the method. – Rand Random Apr 22 '14 at 14:27
  • I missed returning a list of anonymous types - however I would still recommend using generics especially if the user is making use of abstract classes, and wants to query against descending object types. After all, the OP did mention "Generic Data Sets" – Mez Apr 22 '14 at 14:41
0

Here is the reference article, which talks about IList(inherits ICollection( and IEnumerable(Base Generic Interface for IQueryable,ICollection,List).

Here are the links which states generics & it's differences & it's usages,

Difference among IEnumerable , IQueryable, ICollection,IList, List

IEnumerable vs. ICollection vs. IQueryable vs. IList

Looking at your linq, it's about specific object & can be extended further in future. IQueryable is right fit for such scenario, as it gives client to iterate/add/remove items.

Palak Bhansali
  • 731
  • 4
  • 10
0

I think what you have is correct but decide for yourself whether you should use it.

From MSDN: Anonymous Types in the Remarks section:

Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object.

and

To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

sakura-bloom
  • 4,524
  • 7
  • 46
  • 61