Tuesday, 15 January 2013

c# - Need to Include() related entities but no option to do so -


I'm not sure how the title of this question will be, let me explain. I need to select a unit type from my database. It is used to select related institutions, but at the same time select the entities where the unit identifier is equal to one of the IDs in the string array.

My code is as follows:

  list & lt; TSR category & gt; Optional = new list & lt; TSRcategory & gt; (); Foreach (var i.elelectives in client.plit ('& amp;')) {int id = int32.Parse (i); Electives.Add (db.TSRCategories.Find (ID)); }  

This selects TSRCategories properly, which are part of the elective list of ID but does not include related entities. I was using this code:

TSR cognition = dbtrs Categories Include ("Capabilities Capabilities Skills"). ToList ();

But it does not select the selected electives only. What I'm ideally looking for is something like this:

  list & lt; TSRcategory & gt; Optional = new list & lt; TSRcategory & gt; (); Foreach (var i.elelectives in client.plit ('& amp;')) {int id = int32.Parse (i); Electives.Add (db.TSRCategories.Find (ID)); } TSRCategories = Electives. Include ("Competencies.CompetencySkills"). ToList ();  

But of course it can not be done for any reason (I really do not know why this can not be done online in case of searching!). Electives is a string that contains & amp; Separating the IDs in the form of delimiters involves competencies in the category of TSR, including competing skills. Is there really a way to do it effectively and in some lines?

You will find that the affiliate ID will result in a bad query display after a second You can get them once presenting the list (I have assumed the key name here: ElectiveId ):

  var selectiveIds = Client.Electives.Split ('& Amp; ') Select (i = & gt; Int32.Parse (i)) .oire (); Var optional = db.TSRCategories. Include (T = & gt; T. Competition. Select (C = & gt; C. Compensation Skills)). Where (SSR => electives: canteen (SSR.acciliid). Ollist ();  

But one thing to mention is that your id is associated with a delimiter in a string field that is a violation of database normalization instead, instead of using a new junction Create a table, e.g. ClientElectitions , which is a generalized fashion with optional clients associated with a client (ClientId, Elective ID) . It will also simplify your EF recovery code.

Edit

According to the examples, I should use . Select for depth specification of curious loading (no . Many selects or other extension methods).


No comments:

Post a Comment