0

这是我的 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()
            );    
4

3 回答 3

4

Distinct很穷,GroupBy为了胜利:

db.Schools.GroupBy(school => school.name).Select(grp => grp.First());
于 2013-10-03T19:07:39.833 回答
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)
            });  
于 2013-10-03T19:17:20.897 回答
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 )

于 2013-10-03T19:22:19.207 回答