views:

73

answers:

3

is this is proper use of IEnumerable or shell i use list?
what i need to put in <PropertyInfo>?

public static IEnumerable<PropertyInfo> GetNewsList<T>(int FID)
    {
        CatomWebNetDataContext pg = (CatomWebNetDataContext)db.GetDb();
        return (from nls in pg.NewsCat_ITEMs
                  join vi in pg.VIRTUAL_ITEMs on nls.NC_VI_ID equals vi.VI_ID
                  where vi.VI_VF_ID == FID
                  select new { nls, vi });


    }

or

 public List<PropertyInfo> GetPublic<T>(int FID)
    {
        CatomWebNetDataContext pg = (CatomWebNetDataContext)db.GetDb();
        var nl = (from nls in pg.NewsCat_ITEMs
                join vi in pg.VIRTUAL_ITEMs on nls.NC_VI_ID equals vi.VI_ID
                where vi.VI_VF_ID == FID
                select new { nls, vi });

        List<PropertyInfo> retList = new List<PropertyInfo>();

        foreach (var item in nl)
        {
             retList.Add(item);
        }


        return retList;
    }
+1  A: 

The list is an instace of type, that implements IEnumerable. What it means? That if you want to return the IEnumerable<PropertyInfo> you have to create a list (or array etc.) of it and then return it.. From the outside of the method it will look like you are returning IEnumerable<PropertyInfo> but really it will be a List<PropertyInfo>.

About your query... You have to select object of type PropertyInfo, but right now you ae returning some anonymouse type. You should try it like this:

public static IEnumerable<PropertyInfo> GetNewsList<T>(int FID)
{
    CatomWebNetDataContext pg = (CatomWebNetDataContext)db.GetDb();
    var result from nls in pg.NewsCat_ITEMs
               join vi in pg.VIRTUAL_ITEMs on nls.NC_VI_ID equals vi.VI_ID
               where vi.VI_VF_ID == FID
               select new PropertyInfo { SomeMember = nls, SomeOtherMember = vi };

    return result.ToList();
}
ŁukaszW.pl
now i get The type or namespace name 'PropertyInfo' could not be found (are you missing a using directive or an assembly reference?)
eyalb
okay, please show me your updated code ;)
ŁukaszW.pl
public static IEnumerable<newsListItems> GetNewsList<T>(int FID) { CatomWebNetDataContext pg = (CatomWebNetDataContext)db.GetDb(); var result = (from nls in pg.NewsCat_ITEMs join vi in pg.VIRTUAL_ITEMs on nls.NC_VI_ID equals vi.VI_ID where vi.VI_VF_ID == FID select new newsListItems { tt = nls, tt2 = vi }); return result.ToList(); }
eyalb
Are you sure that you have newsListItems class defined somewhere? Can you show me it's code?
ŁukaszW.pl
no, i dont have a class "newsListItems". i thought that i could be a new type of object, do i need to create it? its a join table from my DB. there is another way to return the "result"?
eyalb
No.. you cannot return anonymouse type in a method. You have to create a class that represents your entity (in this case got from joined tables).Also why are you put <T> in definition of your method? You do not use it in it's body, so consider removing it ;)
ŁukaszW.pl
return nls because it most likely contains IEnumerable<VIRTUAL_ITEM>
itchi
+1  A: 

It depends on what you want to do with the result. IEnumerable basically only supports iterating over the results contained in the collection. IList is more specific, and allows you to:

  • Find out how many items are in the collection without iterating through all of them
  • Easily access an item in a specific position in the list
  • Add or remove items from the list

among other things. If you don't need the extra functionality, I would return IEnumerable. The caller of your method can easily do what you did to add the items to a List.

Sean Reilly
A: 

You're able to return a List<PropertyInfo>, given that it's an implementation of IEnumerable<PropertyInfo>. The only thing will be that it will look like a plain old IEnumerable to the caller function once it returns.

Mark LeMoine