比较两个列表并使用linq返回不匹配的项目

时间:2012-08-14 17:33:14

标签: c# linq list c#-4.0

我有两个清单

List<Sent> SentList;
List<Messages> MsgList;

都具有名为MsgID的相同属性;

MsgList            SentList  

MsgID Content      MsgID Content Stauts
1       aaa        1       aaa     0
2       bbb        3       ccc     0
3       ccc        
4       ddd
5       eee

我想将Msglist中的MsgID与发送列表进行比较,并且需要使用linq不在发送列表中的项目

Result 

MsgID Content
2       bbb
4       ddd
5       eee

10 个答案:

答案 0 :(得分:30)

您可以执行以下操作:

HashSet<int> sentIDs = new HashSet<int>(SentList.Select(s => s.MsgID));

var results = MsgList.Where(m => !sentIDs.Contains(m.MsgID));

这将返回MsgListSentList中没有匹配ID的所有邮件。

答案 1 :(得分:14)

天真的方法:

MsgList.Where(x => !SentList.Any(y => y.MsgID == x.MsgID))

请注意,这会占用m*n次操作,因为它会将MsgID中的每个SentListMsgList中的每个{{1}}进行比较(“最多”,因为它会短路当 碰巧匹配时)。

答案 2 :(得分:11)

嗯,你已经有了很好的答案,但他们是大多数Lambda。更多LINQ方法就像

var NotSentMessages =
                from msg in MsgList
                where !SentList.Any(x => x.MsgID == msg.MsgID)
                select msg;

答案 3 :(得分:8)

您可以执行类似

的操作
var notSent = MsgSent.Except(MsgList, MsgIdEqualityComparer);

您需要提供MSDN上列出的自定义相等比较器

http://msdn.microsoft.com/en-us/library/bb336390.aspx

仅在每个相应类型的MsgID属性上具有相等性比较基本相等性。由于相等比较器比较了相同类型的两个实例,因此您需要定义具有 MsgID的已发送消息实现的接口或公共基本类型属性。

答案 4 :(得分:4)

你可以这样做,这是最快的过程

Var result = MsgList.Except(MsgList.Where(o => SentList.Select(s => s.MsgID).ToList().Contains(o.MsgID))).ToList();

这将为您提供预期的输出。

答案 5 :(得分:2)

尝试,

  public class Sent
{
    public int MsgID;
    public string Content;
    public int Status;

}

public class Messages
{
    public int MsgID;
    public string Content;
}

  List<Sent> SentList = new List<Sent>() { new Sent() { MsgID = 1, Content = "aaa", Status = 0 }, new Sent() { MsgID = 3, Content = "ccc", Status = 0 } };
            List<Messages> MsgList = new List<Messages>() { new Messages() { MsgID = 1, Content = "aaa" }, new Messages() { MsgID = 2, Content = "bbb" }, new Messages() { MsgID = 3, Content = "ccc" }, new Messages() { MsgID = 4, Content = "ddd" }, new Messages() { MsgID = 5, Content = "eee" }};

            int [] sentMsgIDs = SentList.Select(v => v.MsgID).ToArray();
            List<Messages> result1 = MsgList.Where(o => !sentMsgIDs.Contains(o.MsgID)).ToList<Messages>();

希望它有所帮助。

答案 6 :(得分:0)

List<Car> cars = new List<Car>() {  new Car() { Name = "Ford", Year = 1892, Website = "www.ford.us" }, 
                                    new Car() { Name = "Jaguar", Year = 1892, Website = "www.jaguar.co.uk" }, 
                                    new Car() { Name = "Honda", Year = 1892, Website = "www.honda.jp"} };

List<Factory> factories = new List<Factory>() {     new Factory() { Name = "Ferrari", Website = "www.ferrari.it" }, 
                                                    new Factory() { Name = "Jaguar", Website = "www.jaguar.co.uk" }, 
                                                    new Factory() { Name = "BMW", Website = "www.bmw.de"} };

foreach (Car car in cars.Where(c => !factories.Any(f => f.Name == c.Name))) {
    lblDebug.Text += car.Name;
}

答案 7 :(得分:0)

作为扩展方法

public static IEnumerable<TSource> AreNotEqual<TSource, TKey, TTarget>(this IEnumerable<TSource> source, Func<TSource, TKey> sourceKeySelector, IEnumerable<TTarget> target, Func<TTarget, TKey> targetKeySelector) 
{
    var targetValues = new HashSet<TKey>(target.Select(targetKeySelector));

    return source.Where(sourceValue => targetValues.Contains(sourceKeySelector(sourceValue)) == false);
}

例如

public class Customer
{
    public int CustomerId { get; set; }
}

public class OtherCustomer
{
    public int Id { get; set; }
}


var customers = new List<Customer>()
{
    new Customer() { CustomerId = 1 },
    new Customer() { CustomerId = 2 }
};

var others = new List<OtherCustomer>()
{
    new OtherCustomer() { Id = 2 },
    new OtherCustomer() { Id = 3 }
};

var result = customers.AreNotEqual(customer => customer.CustomerId, others, other => other.Id).ToList();

Debug.Assert(result.Count == 1);
Debug.Assert(result[0].CustomerId == 1);

答案 8 :(得分:0)

List<Person> persons1 = new List<Person>
           {
                    new Person {Id = 1, Name = "Person 1"},
                    new Person {Id = 2, Name = "Person 2"},
                    new Person {Id = 3, Name = "Person 3"},
                    new Person {Id = 4, Name = "Person 4"}
           };


        List<Person> persons2 = new List<Person>
           {
                    new Person {Id = 1, Name = "Person 1"},
                    new Person {Id = 2, Name = "Person 2"},
                    new Person {Id = 3, Name = "Person 3"},
                    new Person {Id = 4, Name = "Person 4"},
                    new Person {Id = 5, Name = "Person 5"},
                    new Person {Id = 6, Name = "Person 6"},
                    new Person {Id = 7, Name = "Person 7"}
           };
        var output = (from ps1 in persons1
                      from ps2 in persons2
                      where ps1.Id == ps2.Id
                      select ps2.Name).ToList();

人员班

public class Person
{        
    public int Id { get; set; }       

    public string Name { get; set; }
}

答案 9 :(得分:0)

如果您想从第二个列表中选择列表项:

MainList.Where(p => 2ndlist.Contains(p.MainList的列))。ToList();

相关问题