使用元组列表的 linq to object

发布于 2024-10-25 12:05:50 字数 287 浏览 0 评论 0原文

我需要在实体框架中查询对象集合并查找满足参数列表的对象。例如,对象可以包含字段(p1、p2、p3、p4、.. pn)。我想找到满足以下条件的 3 个对象 (p1 = 3, p2 = 7, P3 =23), (p1 = 3, p2 = 43, p3 = 45), (p1 = 1, p2 =232, p3 = 321)。

由于集合可能包含许多元素,并且我可能正在寻找许多(例如 80)这样的元组,因此我不想在循环中执行此操作,因为这需要大量往返。我更愿意用单个 LINQ 语句来完成此操作。这可能吗?

谢谢,

杰瑞

I need to query a object collection in Entity framework and find the objects that meet a list of parameters. For example and object could contain fields (p1, p2, p3, p4, .. pn). I want to find the 3 objects for which the following are true (p1 = 3, p2 = 7, P3 =23), (p1 = 3, p2 = 43, p3 = 45), (p1 = 1, p2 =232, p3 = 321).

Since the collection could contain many elements and I could be looking for many (ex. 80) such tuples I do not want to do this in a loop since that would require a lot of round trips. I would prefer to do this is a single LINQ statement. Is this possible?

Thanks,

Jerry

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

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

发布评论

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

评论(2

尛丟丟 2024-11-01 12:05:57

您可以尝试以下方法,但我不确定它是否适用于 Linq-to-entity。

IEnumerable<Tuple<int, int, int>> criterias = ...;

var filteredObjects = DataContext.YourObjects
                .Join(criterias, 
                      o => new {o.P1, o.P2, o.P3}, // your objects key
                      c => new {o.P1, o.P2, o.P3}, // criteria key
                      (o, c) => o);

You can try following approach, though I'm not sure that it will work with Linq-to-entity.

IEnumerable<Tuple<int, int, int>> criterias = ...;

var filteredObjects = DataContext.YourObjects
                .Join(criterias, 
                      o => new {o.P1, o.P2, o.P3}, // your objects key
                      c => new {o.P1, o.P2, o.P3}, // criteria key
                      (o, c) => o);
千纸鹤 2024-11-01 12:05:57

是的,你可以这样做。只要这样做:

var matches = entities.Where(e => 
       (e.p1 == 3 && e.p2 == 7 && e.p3 == 23)
    || (e.p1 == 3 && e.p2 == 43 && e.p3 == 45)
    || (e.p1 == 1 && e.p2 == 232 && e.p3 == 321));

Yes, you could do this. Just do:

var matches = entities.Where(e => 
       (e.p1 == 3 && e.p2 == 7 && e.p3 == 23)
    || (e.p1 == 3 && e.p2 == 43 && e.p3 == 45)
    || (e.p1 == 1 && e.p2 == 232 && e.p3 == 321));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文