繁体   English   中英

从linq查询C#中删除重复项

[英]remove duplicates from linq query c#

这是我的linq查询,我得到很多重复的学校名称。 所以我创建了一个正则表达式函数来修剪文本:

public static string MyTrimmings(string str)
        {
        return Regex.Replace(str, @"^\s*$\n", string.Empty, RegexOptions.Multiline).TrimEnd();
        }

文本会被修剪,但是,下拉列表中的值都是重复的! 请帮助我消除重复,哦,Linq欢乐!!

ViewBag.schools = new[]{new SelectListItem
            {
                Value = "",
                Text = "All"
            }}.Concat(
            db.Schools.Where(x => (x.name != null)).OrderBy(o => o.name).ToList().Select(s => new SelectListItem
            {
                Value = MyTrimmings(s.name),
                Text = MyTrimmings(s.name)
            }).Distinct()
            );    

Distinct是, GroupBy必胜:

db.Schools.GroupBy(school => school.name).Select(grp => grp.First());

假设您有一个School类,则可以编写IEqualityComparer

class SchoolComparer : IEqualityComparer<School>
{
    public bool Equals(School x, School y)
    {
        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the school' properties are equal. 
        return x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(School school)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(school, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashSchoolName = school.Name == null ? 0 : school.Name.GetHashCode();

        //Calculate the hash code for the school. 
        return hashSchoolName;
    }
}

然后,您的linq查询将如下所示:

db.Schools.Where(x => x.name != null)
          .OrderBy(o => o.name).ToList()
          .Distinct(new SchoolComparer())
          .Select(s => new SelectListItem
            {
                Value = MyTrimmings(s.name),
                Text = MyTrimmings(s.name)
            });  

您可以使您的类实现IEquatable<T>接口,因此Distinct将知道如何进行比较。 像这样(基本示例):

public class SelectListItem : IEquatable<SelectListItem>
{
    public string Value { get; set; }
    public string Text { get; set; }

    public bool Equals(SelectListItem other)
    {
        if (other == null)
        {
            return false;
        }

        return Value == other.Value && Text == other.Text;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 17;

            if (Value != null)
            {
                hash = hash * 23 + Value.GetHashCode();
            }

            if (Text != null)
            {
                hash = hash * 23 + Text.GetHashCode();
            }

            return hash;
        }
    }
}

(GetHashCode是John Skeet的回答在这里: https : //stackoverflow.com/a/263416/249000

暂无
暂无

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

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