使用linq和entityframework合并多个select的结果

时间:2018-06-01 06:48:09

标签: entity-framework linq parent-child

如何合并这3行的结果

var newscatid=Dbcontext.tbl_NewsPosition.where(x => x.Fk_NewsID==4 and     IsMainPosition=true).select(x => x.Fk_NewsCatId);

from p in Dbcontext.tbl_cat.where(x => x.Id== newscatid) select new {    parentCat = b.CatName};
from ch in Dbcontext.tbl_cat.where(x => x.Fk_ParentId== newscatid) select new { childCat = ch.CatName};

这就是我想要获得的:

+-----------+----------+
| parentCat | childCat |
+-----------+----------+
| Sport     | Footbal  |
|           |          |
+-----------+----------+

这些是我的表格:

enter image description here

tbl_NewsPosition

2 个答案:

答案 0 :(得分:1)

试试这个:

animal_name

答案 1 :(得分:1)

请参考以下代码:

   var newscatid=Dbcontext.tbl_NewsPosition.where(x => x.Fk_NewsID==4 and     
   IsMainPosition=true).select(x => x.Fk_NewsCatId);'

    var data = 
    from p in Dbcontext.tbl_cat
    join ch in Dbcontext.tbl_cat on p.Id equals ch.Fk_ParentId
    where p.Id==newscatid
    select new 
    { 
      parentCat  = p.CatName , 
      childCat  = ch.CatName
    };