如果我有一个可用的参数,也将在where子句中使用,则需要使用Lambda添加连接。
我的问题是我不确定添加新对象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 });
}
我想你应该这样做:
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
就足够了,并且可以通过精心选择的属性名(如您所拥有的)更好地可读性。