我需要使用Lambda添加连接,如果我有另一个可用的参数,也将在where子句中使用。
我的问题是我不确定添加新对象MemberTagLysts的确切格式,以及如何创建where子句。
var tagList = from t in dc.Tags
join b in dc.Businesses on t.BusinessId equals b.BusinessId
where t.IsActive == true
where b.IsActive == true
orderby t.AdImage descending
select new TagItem
{
tagName = t.Name.Replace("\"", ""),
tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
tagDescription = t.Description.Replace("\"", "")
};
if (!string.IsNullOrEmpty(lystId))
{
tagList = (IQueryable<TagItem>)tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });
}
答案 0 :(得分:1)
我想你想做这样的事情:
var tagList = from t in dc.Tags
join b in dc.Businesses on t.BusinessId equals b.BusinessId
where t.IsActive
where b.IsActive
orderby t.AdImage descending
select new TagItem
{
tagName = t.Name.Replace("\"", ""),
tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
tagDescription = t.Description.Replace("\"", "")
};
if (!string.IsNullOrEmpty(lystId))
{
tagList = tagList
.GroupJoin(dc.MemberTagLysts.Where(l => l.lystId == lystId),
a => a.tagId,
b => b.TagId,
(a, b) => new { a, b }));
}
有条件地扩展查询是一种很好的做法。请注意,像where t.IsActive == true
这样的条件是多余的,where t.IsActive
就足够了,可以通过精心选择的属性名称(如您所知)更好地阅读。