c# - Return anonymous type results? -


using simple example below, best way return results multiple tables using linq sql?

say have 2 tables:

dogs:   name, age, breedid breeds: breedid, breedname 

i want return dogs breedname. should dogs using no problems:

public iqueryable<dog> getdogs() {     var db = new dogdatacontext(connectstring);     var result = d in db.dogs                  join b in db.breeds on d.breedid equals b.breedid                  select d;     return result; } 

but if want dogs breeds , try have problems:

public iqueryable<dog> getdogswithbreednames() {     var db = new dogdatacontext(connectstring);     var result = d in db.dogs                  join b in db.breeds on d.breedid equals b.breedid                  select new                         {                             name = d.name,                             breedname = b.breedname                         };     return result; } 

now realize compiler won't let me return set of anonymous types since it's expecting dogs, there way return without having create custom type? or have create own class dogswithbreednames , specify type in select? or there easier way?

i tend go pattern:

public class dogwithbreed {     public dog dog { get; set; }     public string breedname  { get; set; } }  public iqueryable<dogwithbreed> getdogswithbreednames() {     var db = new dogdatacontext(connectstring);     var result = d in db.dogs                  join b in db.breeds on d.breedid equals b.breedid                  select new dogwithbreed()                         {                             dog = d,                             breedname = b.breedname                         };     return result; } 

it means have class, it's quick , easy code, extensible, reusable , type-safe.


Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -