CharlieShi
CharlieShi

Reputation: 916

How to deal with nested entities by using LINQ?

I now have an Entity named Basdiseasecure with ComdrugList in it:

public class Basdiseasecure
{
    public Guid               Disease_cure_id { get; set; }    //pk
    public Guid?              Eartag_id       { get; set; }   
    public DateTime?          Cure_time       { get; set; } 
    public string             Cure_method     { get; set; }
    public string             Cure_result     { get; set; }
    public string             Combdrug_idlist { get; set; }

    public List<Bascombdrug> ComdrugList { get; set; } 
}

The Bascombdrug entity lists below:

public class Bascombdrug
{
    public Guid      Combdrugid   { get; set; } //pk
    public string    Combdrugname { get; set; }  
    public string    Combdrugnote { get; set; }  
    public DateTime  Updatetime   { get; set; }  
}

In entity Basdiseasecure, the Combdrug_idlist stores the Combdrugid and split them by comma(like F47EF316-2308-4673-AFA4-D5FA81E520D8,F47EF316-2308-4673-AFA4-D5FA81E52000). Below is the code I used to get Basdiseasecure entity by Disease_cure_id:

[WebMethod]
public List<Basdiseasecure> GetDiseaseCureInfo(Guid id)
{
      IQueryable<Basdiseasecure> getDiseaseCure;
      using (GDeerGardenEntities entities = new GDeerGardenEntities())
      {
              getDiseaseCure = from p in entities.bus_disease_cure
                               select new Basdiseasecure()
                               {
                                   Disease_cure_id = p.disease_cure_id,
                                   Eartag_id = p.eartag_id,
                                   Cure_time = p.cure_time,
                                   Cure_method = p.cure_method,
                                   Cure_result = p.cure_result,
                                   Combdrug_idlist = p.combdrug_idlist
                                   //How can I build ComdrugList here ?
                               };
          return getDiseaseCure.ToList();
     }
}

I don't know how to build my List for Bascombdrug based on the Combdrug_idlist field. Is there any good way to do this? thx.

Edit

Here I got a method:

First return the list without ComdrugList inside.

Second loop the Combdrug_idlist field and get the ids

Third using linq to extract the releated info by using id one by one and form them into listed entities.

Above is the method I currently use, I need to code much for this scenario. that's why I asked the question.

Upvotes: 2

Views: 57

Answers (1)

Alex Filipovici
Alex Filipovici

Reputation: 32541

Try this (assuming that entities.bus_comb_drug is the source collection which is being used to create the Bascombdrug entities):

getDiseaseCure =
    from p in entities.bus_disease_cure
    select new Basdiseasecure()
    {
        // other properties
        ComdrugList =
        (from d in entities.bus_comb_drug
            join ids in p.combdrug_idlist.Split(',') on
                d.combdrugid.ToString().ToLower() equals ids.ToLower()
            select new Bascombdrug()
            {
                Combdrugid = d.combdrugid
                // other properties
            }).ToList()
    };

Upvotes: 1

Related Questions