c# - linq select many issue -


i have person table , colour table. each person has many colours.

var data = dc.people.include(c => c.colours)     .select(i => new {          i.personid,          fullname = i.firstname + " " + i.lastname,         i.isauthorised,          i.isenabled,          i.colours     })     .orderbydescending(i => i.personid)     .tolist(); 

lets want show 1, james smith, true, true, red , black.

the above code shows me error because i.colours list.

i tried change colour = i.colours.selectmany(c => c.name). gave me error. how can achieve goal?

colours icollection inside person class. public virtual icollection colours { get; set; }

here table diagram: enter image description here

ok, there example 3 different solutions. main problem there no methods string.join mapped sql-query, u have

1) entities db use string.join , on

or

2) use sql-mapped functions

i'll past ef entites.

result output of code is:

getwithanonymousclass:

1, james smith, true, true, red , black

2, thomas anderson, true, true, green , white

getbyobject:

1, james smith, true, true, red , black

2, thomas anderson, true, true, green , white

getbystring:

1, james smith, true, true, red , black

2, thomas anderson, true, true, green , white

example code:

internal class program     {         private static void main(string[] args)         {             using (var dc = new peoplecontext("dbconnection"))             {                 //adding entities example                 //addjamessmith(dc);                 //addthomasanderson(dc);                  getwithanonymousclass(dc);                 getbyobject(dc);                 getbystring(dc);             }              console.readline();         }          /// <summary>         /// example shows how select entity children anonymous class         /// plus: u can entity fields u need         /// </summary>         private static void getwithanonymousclass(peoplecontext dc)         {             console.writeline("getwithanonymousclass:");             var array = dc.people.select(c => new {c.personid, c.firstname, c.lastname, c.isauthorised, c.isenabled, colours = c.colours.select(cc => cc.name)});             foreach (var c in array)                 console.writeline($"{c.personid}, {c.firstname} {c.lastname}, {c.isauthorised}, {c.isenabled}, {string.join(" , ", c.colours)}");             console.writeline();         }          /// <summary>         /// example shows how select entity children , use @ client (console.writeline here)         /// </summary>         /// <param name="dc"></param>         private static void getbyobject(peoplecontext dc)         {             console.writeline("getbyobject:");             var array = dc.people.include(c => c.colours);             foreach (var c in array)                 console.writeline($"{c.personid}, {c.firstname} {c.lastname}, {c.isauthorised}, {c.isenabled}, {string.join(" , ", c.colours.select(cc => cc.name))}");             console.writeline();         }          /// example shows how select entity children string formatted @ sql-server side         /// plus: code execuded @ sql-server         /// minus: u must specify each child explicitly. u can't method, if u don know number of children         private static void getbystring(peoplecontext dc)         {             console.writeline("getbystring:");             var array = dc.people.select(c => c.personid + ", " + c.firstname + " " + c.lastname + ", " + c.isauthorised +                                               ", " + c.isenabled + ", " + c.colours.orderby(сс => сс.colourid).firstordefault().name +                                               " , " + c.colours.orderby(сс => сс.colourid).skip(1).firstordefault().name);              foreach (var c in array)                 console.writeline(c);             console.writeline();         }          private static void addjamessmith(peoplecontext dc)         {             dc.people.add(                 new person                 {                     firstname = "james",                     lastname = "smith",                     isauthorised = true,                     isenabled = true,                     isvalid = true,                     colours = new list<colour>                     {                         new colour {isenabled = true, name = "red"},                         new colour {isenabled = true, name = "black"}                     }                 });             dc.savechanges();         }          private static void addthomasanderson(peoplecontext dc)         {             dc.people.add(                 new person                 {                     firstname = "thomas",                     lastname = "anderson",                     isauthorised = true,                     isenabled = true,                     isvalid = true,                     colours = new list<colour>                     {                         new colour {isenabled = true, name = "green"},                         new colour {isenabled = true, name = "white"}                     }                 });             dc.savechanges();         }     }       public class peoplecontext : dbcontext     {         public peoplecontext(string connectionstringname) : base(connectionstringname)         {         }          public dbset<person> people { get; set; }         public dbset<colour> colours { get; set; }     }      public class person     {         [key]         public int personid { get; set; }          public string firstname { get; set; }          public string lastname { get; set; }          public bool isauthorised { get; set; }         public bool isvalid { get; set; }         public bool isenabled { get; set; }          public list<colour> colours { get; set; }     }      public class colour     {         [key]         public int colourid { get; set; }          public string name { get; set; }          public bool isenabled { get; set; }          public list<person> people { get; set; }     } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -