使用两个表中的LINQ过滤数据! [英] Filter data using LINQ from two tables!
本文介绍了使用两个表中的LINQ过滤数据!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子:
表1:物品
< pre lang =SQL> ItemsId Name_En
1 Cream
2 Oil
3 洗涤剂
4 肥皂
表2:ItemsCommon
ItemsCommonId ItemsId Name_En CategoryId
1 1 面霜 3
2 2 头油 3
3 1 护脚霜 4
4 2 身体油 4
现在什么我想如果用户通过categoryid = 3搜索项目,那么它将显示如下结果:
ItemsId Name_En
1 面霜
2 头油
3 洗衣液
4 肥皂
或按categoryid = 4搜索项目然后结果将be:
ItemsId Name_En
1 Foot Cream
2 身体油
3 洗涤剂
4 肥皂
但如果用户搜索除3,4之外的其他类别,则结果将与项目表:
ItemsId Name_En
1 奶油
2 油
3 洗涤剂
4 肥皂
我尝试这种方法..
public List< Items> SearchItemsByCategory( int categoryid)
{
var items =(来自 i db.Items
join ic in i.ItemsId上的db.ItemsCommon等于ic.ItemsCommonId
into tempJoin
来自 t2 tempJoin.DefaultIfEmpty()
选择 new {t2.Id,t2.Name,t2.ItemsId,t2.CategoryId})。ToList();
返回项;
// var result = casePartyRoles.Where(c => c。)
}
任何建议都真正适用!
解决方案
我自己解决了!< br />
public 列表< items> SearchItemsByCategory( int categoryid)
{
var items =(来自 i db.Items
join ic db.ItemsCommon.Where(x => x.CategoryId == categoryid)i.ItemsId上的
等于ic.ItemsId
into tempJoin
来自 t2 tempJoin .DefaultIfEmpty()
选择 new {i.ItemsId,Name_En = t2 == null ?i.Name_En:t2.Name_En})。ToList();
返回项;
// var result = casePartyRoles.Where(c => c。)
} < / items > ; 跨度>
I have two table :
Table 1 : Items
ItemsId Name_En
1 Cream
2 Oil
3 Detergent
4 Soap
Table 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
Now what I want if user search items by categoryid = 3 then it will show result like :
ItemsId Name_En
1 Face Cream
2 Head Oil
3 Detergent
4 Soap
or search items by categoryid = 4 then result will be :
ItemsId Name_En
1 Foot Cream
2 Body Oil
3 Detergent
4 Soap
but if user search by other categoryid except 3, 4 then result will be same as Items table :
ItemsId Name_En
1 Cream
2 Oil
3 Detergent
4 Soap
I try this method..
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.)
}
Any suggestion really appreaciated !
解决方案
I solve it by self !<br />
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.) }</items>
这篇关于使用两个表中的LINQ过滤数据!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文