简体   繁体   中英

Linq query join one table row to another table multiple row

Table name: Namelist


id | Name
------------
1  |xxxxx
2  |yyyyy

Table name: Category


id | Nameid |Categoryid
-----------------
1  |1       |5
2  |1       |4
3  |2       |3
4  |2       |8

I need a linq query result like this


id | Name   |Categoryid
-----------------
1  |xxxx       |5,4
2  |yyyy       |3,8

I tried below linq but it displays first category id only

var list = from n in namelist 
           join c in category on n.id equals c.nameid 
           select new 
           { 
              n.id,
              n.name,
              c.categoryid
           }

You can do this with Group Join and join all the category id's in the group with String.Join like this:-

 var result = (from n in namelist
              join c in categories 
              on n.Id equals c.NameId into g
              select new 
                       {
                          id = n.Id,
                          Name = n.Name,
                          CategorieIds = g.Select(x => x.CategoryId)
                       }).AsEnumerable()
                         .Select(x => new 
                                   { 
                                     Id = x.id, 
                                     Name = x.Name, 
                                     CategoryIds = String.Join(",",x.CategorieIds)) 
                                   });

You can try String.Join :

var list = namelist
    .Select(n => new { 
        n.id,
        n.name,
        categoryids = String.Join(",", category
            .Where(c => c.nameid == n.id)
            .Select(c => c.categoryid))
    });

Use String.Join() . I modified your statement:

var list = from n in namelist 
           join c in category on n.id equals c.nameid 
           group g by new { n.id, n.name }
           select new 
           { 
              id = g.Key.id,
              Name = g.Key.name,
              Categoryid = String.Join(",", g.Select(x => x.c.categoryid))
           }

First you take a join on NameList and Category table and then bring them into objects using ToList or AsEnumerable methods like

var list = (from n in db.NameList 
           join c in db.Category on n.id equals c.nameid 
           select new 
           { 
              id = n.id,
              Name = n.name,
              Categoryid = c.id
           }).ToList();
var list2 = from l in list
group by new {l.id, l.Name} into groupings
from g in groupings select new{
   g.Key.id,
   g.Key.Name,
   CategoryId = string.Joing(",", groupings.Where(x=>x.NameId == g.Key.id).Select(y=>y.CategoryId))
};

The benefit of using ToList for fetching data from db is that you will only one query in database and all required records are fetched in the memory. The second statement will group those records by id and Name and will apply string.Join on CategoryId . Please note that if you use string.join method on Linq-to-Entities query, it will fail because this method cannot be converted into sql expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM