IEnumerable vs List

Is this the correct use IEnumerableor shell that I use List?
What do 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;
    }
+3
source share
3 answers

A list is a type of type that implements IEnumerable. What does it mean? If you want to return IEnumerable<PropertyInfo>, you need to create a list (or array, etc.), and then return it. From the outside of the method, it will look like you are returning IEnumerable<PropertyInfo>, but actually it will be a List<PropertyInfo>.

... PropertyInfo, anonymouse. :

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();
}
+3

, . IEnumerable , . IList :

  • , .

. , IEnumerable. , , .

+3

You can return List<PropertyInfo>, given that this is an implementation IEnumerable<PropertyInfo>. The only thing is that it will look like the usual old one IEnumerablefor the calling function after it returns.

0
source

Source: https://habr.com/ru/post/1759067/


All Articles