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

时间:2013-10-03 18:58:21

标签: c# regex linq entity-framework

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

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

文本变得很好,但是,下拉值都是重复的!请帮我消除重复,哦林奇欢乐!!

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

3 个答案:

答案 0 :(得分:4)

Distinct很差,胜利为GroupBy

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

答案 1 :(得分:0)

假设你有一个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)
            });  

答案 2 :(得分:0)

您可以让您的类实现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