在 .NET 中按条件列出交集

发布于 2024-12-19 14:40:42 字数 764 浏览 1 评论 0 原文

这是我的观点: 这是数据类:

class Data
{
public string name; 
public int version;
}

这是数据列表:

Ilist<Data> List_old = {new Data{name = "1", version = 1}, new Data{name = "2", version = 1}};
Ilist<Data> List_new = {new Data{name = "1", version = 2}, new Data{name = "3", version = 1}, new Data{name = "2", version = 1}};

结果我需要一个元组列表 - IList> - 第一个数据对象来自 List_new,第二个数据对象来自 List_old,由标准 - 数据对象具有相同的名称,但 - 首先具有比第二个更高的版本,或者如果在 List_old 中没有任何来自 List_new 的同名数据项,则第二个为空。 考虑到结果应该是:

IList<Tuple<Data, Data>> result = { {List_new[0], List_old[0]}, {List_new[1], null}};

您能帮忙编写可以执行该操作的 LINQ 代码吗? 我为此伤透了脑筋...

Here is my point:
here is data class:

class Data
{
public string name; 
public int version;
}

here is data lists:

Ilist<Data> List_old = {new Data{name = "1", version = 1}, new Data{name = "2", version = 1}};
Ilist<Data> List_new = {new Data{name = "1", version = 2}, new Data{name = "3", version = 1}, new Data{name = "2", version = 1}};

As result I need a list of tuples - IList<Tuple<Data, Data>> - first data object is from List_new, second from List_old, taken by criteria - data objects have the same name but - first have greater version that the second OR second is null if there in List_old there is no any data item with the same name from List_new.
Considering that the result should be:

IList<Tuple<Data, Data>> result = { {List_new[0], List_old[0]}, {List_new[1], null}};

could You help with the LINQ code which could perform that?
I blow my brains with this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2024-12-26 14:40:42

如果我正确地解释了你的问题:

var q = 
    (from n in List_new
    join tmp in List_old on n.name equals tmp.name into g
    from o in g.DefaultIfEmpty()
    where o == null || o.version < n.version
    select new Tuple<Data, Data>(n, o)).ToList();

If I've interpreted your question correctly:

var q = 
    (from n in List_new
    join tmp in List_old on n.name equals tmp.name into g
    from o in g.DefaultIfEmpty()
    where o == null || o.version < n.version
    select new Tuple<Data, Data>(n, o)).ToList();
南渊 2024-12-26 14:40:42

谢谢你们 - 但看来我的两个问题都得到了答案:
LINQ 代码:

IList<Tuple<Data,Data> > UpdateList = List_new.Where(
delegate(Data item)
{
return List_old.Any(y => item.name == y.name && item.Version > y.Version) ||
!List_old.Any(y => item.name == y.name);
}
).Select(
delegate(Data item)
{
Data old_item = List_old.FirstOrDefault(y => item.name == y.name && item.Version > y.Version);
return new Tuple<Data,Data>(item, old_item);
}
).ToList();

用于在 .NET 2.0-3.5 中使用 Tuple - 请查看此处

Thank You guys - but it seems that I got answer to both my questions:
LINQ code:

IList<Tuple<Data,Data> > UpdateList = List_new.Where(
delegate(Data item)
{
return List_old.Any(y => item.name == y.name && item.Version > y.Version) ||
!List_old.Any(y => item.name == y.name);
}
).Select(
delegate(Data item)
{
Data old_item = List_old.FirstOrDefault(y => item.name == y.name && item.Version > y.Version);
return new Tuple<Data,Data>(item, old_item);
}
).ToList();

for using Tuple in .NET 2.0-3.5 - look here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文