【问题标题】:get the entire row using distinct on one column in LINQ在 LINQ 的一列上使用 distinct 获取整行
【发布时间】:2013-10-17 09:10:40
【问题描述】:

我需要在LINQ 的一列上使用distinct 检索整个行集合。

我正在使用GroupBy 获取Distinct,但它只选择一列值而不是我需要整行。

**此列表返回在 ThumbnailAltText 列中唯一的集合,但仅返回该列而不是整行值。所以我不得不使用var 而不是type

 var catlist = _db.ac_Categories
    .Where(c => c.VisibilityId == 0 && c.ThumbnailAltText != null
        && (!c.ThumbnailAltText.StartsWith("gifts/")
        && !c.ThumbnailAltText.StartsWith("email/")
        && !c.ThumbnailAltText.StartsWith("news/")
        && !c.ThumbnailAltText.StartsWith("promotions/")
        && !c.ThumbnailAltText.StartsWith("the-knowledge/"))).ToList()
        .GroupBy(c=> c.ThumbnailAltText.Trim().Distinct()).ToList();
                                .GroupBy(c=> c.ThumbnailAltText.Trim().Distinct()).ToList();

同样的事情不适用于像这样的type,我遇到了错误。

List<ac_Categories> catlist = _db.ac_Categories
        .Where(c => c.VisibilityId == 0 && c.ThumbnailAltText != null
            && (!c.ThumbnailAltText.StartsWith("gifts/")
            && !c.ThumbnailAltText.StartsWith("email/")
            && !c.ThumbnailAltText.StartsWith("news/")
            && !c.ThumbnailAltText.StartsWith("promotions/")
            && !c.ThumbnailAltText.StartsWith("the-knowledge/"))).ToList()
            .GroupBy(c=> c.ThumbnailAltText.Trim().Distinct()).ToList();
                                    .GroupBy(c=> c.ThumbnailAltText.Trim().Distinct()).ToList();

错误:The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.

编辑:我需要一个集合而不是第一条记录,ID 和其他列仅包含差异值 ThumbnailAltText 我包含重复项

【问题讨论】:

  • 能否提供ac_Categories类型定义
  • 它是使用数据库优先方法创建的实体...它是一个非常大的表,因此无法在此处提供...

标签: c# asp.net linq distinct


【解决方案1】:

Distinct 在字符串上返回唯一字符。我假设您想返回所有行,但根据ThumbnailAltText,每一行都应该是唯一的,对吗?

那么这应该可以工作,它只返回每个组的第一行:

var catlist = _db.ac_Categories
    .Where(c => c.VisibilityId == 0 && c.ThumbnailAltText != null
        && (!c.ThumbnailAltText.StartsWith("gifts/")
        && !c.ThumbnailAltText.StartsWith("email/")
        && !c.ThumbnailAltText.StartsWith("news/")
        && !c.ThumbnailAltText.StartsWith("promotions/")
        && !c.ThumbnailAltText.StartsWith("the-knowledge/")))
     .GroupBy(c=> c.ThumbnailAltText.Trim())
     .ToList()
     .Select(g => g.First())
     .ToList();

【讨论】:

  • 这是返回整行还是只返回一列值?
  • @patel.milanb:它返回整行。
  • 让我试一试,然后告诉你......所以如果它返回整行,我可以使用返回类型作为 List 对吗?而不是 var
  • 这给了我错误:方法 'First' 只能用作最终查询操作。请考虑在此实例中使用“FirstOrDefault”方法。
  • @patel.milanb:然后尝试在 Select 之前添加 ToList,就像您在代码中所做的那样(编辑了我的答案)。
【解决方案2】:

你也可以像这样使用Distinct(IEnumerable, IEqualityComparer) 扩展名

public class CategotiesEqualityComparer : IEqualityComparer<ac_Categories>
{
    public bool Equals(ac_Categories x, ac_Categories y)
    {
        return x.ThumbnailAltText.Trim() == y.ThumbnailAltText.Trim();
    }

    public int GetHashCode(ac_Categories obj)
    {
        return obj.ThumbnailAltText.Trim().GetHashCode();
    }
}

List<ac_Categories> catlist = _db.ac_Categories
    .Where(c => c.VisibilityId == 0 && c.ThumbnailAltText != null
        && (!c.ThumbnailAltText.StartsWith("gifts/")
        && !c.ThumbnailAltText.StartsWith("email/")
        && !c.ThumbnailAltText.StartsWith("news/")
        && !c.ThumbnailAltText.StartsWith("promotions/")
        && !c.ThumbnailAltText.StartsWith("the-knowledge/")))
    .Distinct(new CategotiesEqualityComparer())
    .ToList()

【讨论】:

  • 错误:LINQ to Entities 无法识别方法“System.Linq.IQueryable1[WebMgr.ac_Categories] Distinct[ac_Categories](System.Linq.IQueryable1[WebMgr.ac_Categories], System.Collections.Generic.IEqualityComparer`1[WebMgr.ac_Categories])”方法,并且该方法不能翻译成商店表达式。
  • IEnumerable 的扩展,而不是 IQueryable
  • 是的...我明白了...我必须使用 AsEnumerable()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多