【问题标题】:linq groupjoin using lambda with a where clauselinq groupjoin 使用带有 where 子句的 lambda
【发布时间】:2013-01-13 03:40:08
【问题描述】:

如果我还有一个可用的参数也将在 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 });
            }

【问题讨论】:

  • 您应该向我们提供有关您问题的更多背景信息。 MemberTagLysts 是什么?您的查询的预期输出是什么?你想达到什么目标?

标签: linq join lambda


【解决方案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 就足够了,而且通过精心选择的属性名称(正如您所拥有的那样)可以更好地阅读。

【讨论】:

  • Gert 谢谢,但是使用那个代码,我得到了这个错误... --> “CS1501:方法 'Where' 没有重载需要 4 个参数”
猜你喜欢
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2020-05-25
  • 2012-11-12
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多