Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use List<dynamic>

Tags:

c#

list

dynamic

I need to get the list of billers from my database. here is my code:

public static List<dynamic> GetBillers()
{
    DataLayer.DLContext context = new DataLayer.DLContext();

    var Billers = (from b in context.Biller
                   where b.IsActive == true && b.IsDeleted == false
                   select new
                   {
                       ID = b.ID,
                       DisplayName = b.DisplayName
                   }).ToList();

    return Billers;
}

I am getting this error:

Cannot implicitly convert type Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.List<dynamic>

Please help.

like image 646
aianLee Avatar asked May 14 '26 22:05

aianLee


2 Answers

(from b in context.Biller
 where b.IsActive == true && b.IsDeleted == false
 select (dynamic) new {
     ID = b.ID,
     DisplayName = b.DisplayName
 }).ToList();

should do the job. Personally, though, I'm not sure it is a particularly helpful move - I would suggest returning a known class / interface instead. dynamic has various uses, but this isn't a good one.

like image 90
Marc Gravell Avatar answered May 17 '26 12:05

Marc Gravell


Cast to dynamic:

var Billers = (from b in context.Biller
               where b.IsActive == true && b.IsDeleted == false
               select new
               {
                   ID = b.ID,
                   DisplayName = b.DisplayName
               }).ToList<dynamic>();
like image 34
Alberto Avatar answered May 17 '26 10:05

Alberto