我有两张桌子:
表1:项目
ItemsId Name_En
1 Cream
2 Oil
3 Detergent
4 Soap
表2:ItemsCommon
ItemsCommonId ItemsId Name_En CategoryId
1 1 Face Cream 3
2 2 Head Oil 3
3 1 Foot Cream 4
4 2 Body Oil 4
现在我想要的是,如果用户通过categoryid = 3搜索项目,那么它将显示如下结果:
ItemsId Name_En
1 Face Cream
2 Head Oil
3 Detergent
4 Soap
或按categoryid = 4搜索项目,结果将是:
ItemsId Name_En
1 Foot Cream
2 Body Oil
3 Detergent
4 Soap
但如果用户搜索除3,4之外的其他categoryid,则结果将与Items表相同:
ItemsId Name_En
1 Cream
2 Oil
3 Detergent
4 Soap
我尝试这种方法..
public List<Items> SearchItemsByCategory(int categoryid )
{
var items= (from i in db.Items
join ic in db.ItemsCommon on i.ItemsId equals ic.ItemsCommonId
into tempJoin
from t2 in tempJoin.DefaultIfEmpty()
select new { t2.Id,t2.Name,t2.ItemsId,t2.CategoryId }).ToList();
return items;
// var result = casePartyRoles.Where(c=>c.)
}
任何建议确实都是适用的!
答案 0 :(得分:1)
试试这个:
public List<Items> SearchItemsByCategory(int categoryid )
{
var items= (from i in db.Items
join ic in db.ItemsCommon.Where(x=>x.CategoryId == categoryid )
on i.ItemsId equals ic.ItemsId
into tempJoin
from t2 in tempJoin.DefaultIfEmpty()
select new { i.ItemsId,Name_En = t2==null?i.Name_En: t2.Name_En }).ToList();
return items;
// var result = casePartyRoles.Where(c=>c.)
}