从列表中获取唯一记录,而不会影响列表

时间:2016-06-01 07:38:46

标签: c# linq

我正在尝试从列表中获取不同的记录。我的代码正在获取不同的记录,但它正在改变列表的顺序。如何在不影响数据顺序的情况下获取记录。

我正在使用此代码

var data= data1.GroupBy(x => x.CourseId).Select(g => g.First());

var data = data1.GroupBy(c => new { CourseId = c.CourseId, CourseName = c.CourseName })
                .Select(c => new { CourseId = c.Key.CourseId, CourseName = c.Key.CourseName });

两个查询都给出相同的输出顺序

请帮忙! 谢谢! 迪皮卡乌达

4 个答案:

答案 0 :(得分:2)

您应该使用distinct()EqualityComparer

class CourseDetails
{
    public CourseDetails(int id)
    {
        Id = id;
    }
    public int Id { get; }
}

class CourseDetailsEqualityComparer : EqualityComparer<CourseDetails>
{
    public override bool Equals(CourseDetails x, CourseDetails y)
    {
        return x.Id == y.Id;
    }

    public override int GetHashCode(CourseDetails obj)
    {
        return obj.Id.GetHashCode();
    }
}

[TestMethod]
public void FindDistinct()
{
    var courses = new List<CourseDetails> { 
        new CourseDetails(4),
        new CourseDetails(2),
        new CourseDetails(3),
        new CourseDetails(2),
        new CourseDetails(3),
        new CourseDetails(1),
        new CourseDetails(1),
    };

    var expected = new List<CourseDetails> {
        new CourseDetails(4),
        new CourseDetails(2),
        new CourseDetails(3),
        new CourseDetails(1),
    };

    var distinct = courses.Distinct(new CourseDetailsEqualityComparer()).ToList();

    CollectionAssert.AreEqual(expected, distinct, new CourseDetailsComparer());
}

class CourseDetailsComparer : Comparer<CourseDetails>
{
    public override int Compare(CourseDetails x, CourseDetails y)
    {
        return x.Id.CompareTo(y.Id);
    }
}

答案 1 :(得分:0)

我不是100%它会回答你的问题,但你可以尝试&#34; .Distinct()&#34;功能在C#。

var data = data1.Distinct(); 要么 var data = data1.Distinct(x =&gt; x.CourseId);

答案 2 :(得分:0)

添加一行新代码:

data  = (from o in data order by o.sortproperty Ascending).toList();

答案 3 :(得分:0)

您可以尝试这个经过测试的代码。它会在不更改原始订单的情况下提供不同项目的列表。

 class Program
    {
        static void Main(string[] args)
        {
            List <Person> P_list = new List<Person>();
            P_list.Add(new Person("Jack", "Harvey", "Smyrna, GA 30080"));
            P_list.Add(new Person("Tyler", "Marsden", "Iowa City, IA 52240"));
            P_list.Add(new Person("Callum", "Richardson ", "Marquette, MI 49855"));
            P_list.Add(new Person("Taylor", "Craig ", "Logan, UT 84321"));
            P_list.Add(new Person("Callum", "Richardson ", "Marquette, MI 49855"));
            P_list.Add(new Person("Tyler", "Marsden", "Iowa City, IA 52240"));
            P_list.Add(new Person("William", "Donnelly", "Richmond, IN 47374"));
            P_list.Add(new Person("Callum", "Richardson ", "Marquette, MI 49855"));
            P_list.Add(new Person("Jack", "Harvey", "Smyrna, GA 30080"));
            P_list.Add(new Person("Billy", "Reid ", "New York, NY 10029"));            

            IEnumerable<Person> distinct = P_list.Distinct(new PersonComparer());
            foreach (var item in distinct)
            {
                Console.WriteLine("{0} {1} {2}", item.FName,item.LName,item.Location);
            }
            Console.Read();
        }

    }

    class Person
    {
        public string FName { get; set; }
        public string LName { get; set; }
        public string Location { get; set; }
        public Person(string f, string l, string loc)
        {
            FName = f;
            LName = l;
            Location = loc;
        }
    }

    public class PersonComparer : IEqualityComparer<Person>
    {
        bool IEqualityComparer<Person>.Equals(Person x, Person y)
        {
            return (x.FName == y.FName & x.LName == y.LName && x.Location == y.Location);
        }

        int IEqualityComparer<Person>.GetHashCode(Person obj)
        {
            string person = string.Format("{0} {1} {2}", obj.FName, obj.LName, obj.Location);
            return person.GetHashCode();
        }
    }