简体   繁体   English

在LINQ中的一列上使用distinct来获取整行

[英]get the entire row using distinct on one column in LINQ

I need to retrieve the entire rows collection using distinct on one column in LINQ . 我需要使用LINQ一列上的distinct来检索整个行集合。

i am getting the Distinct using GroupBy but it only selects the one column values instead i need entire row. 我使用GroupBy得到了Distinct但它只选择了一列值,而我需要整行。

**this list returns the collection which is unique in ThumbnailAltText column BUT only returns that column rather than entire row values. **此列表返回ThumbnailAltText列中唯一的集合BUT仅返回该列而不是整行值。 so i had to use var instead of type 所以我不得不使用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();

same thing does not work with type like this and i am getting error. 同样的事情不适用于这样的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();

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

EDIT: I need a collection and not the first record, ID and other columns contains diff values only ThumbnailAltText my contain duplicates 编辑:我需要一个集合而不是第一个记录,ID和其他列只包含diff值ThumbnailAltText我包含重复项

Distinct on a string returns unique characters. 字符串的Distinct返回唯一字符。 I assume you want to return all rows, but every row should be unique according to the ThumbnailAltText , is that correct? 我假设您想要返回所有行,但根据ThumbnailAltText ,每一行都应该是唯一的,这是正确的吗?

Then this should work, it returns simply the first row of each group: 然后这应该工作,它只返回每组的第一行:

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();

also you may use Distinct(IEnumerable, IEqualityComparer) extension like this 你也可以像这样使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM