实体框架 - 在同一列的多个条件选择 - 引用表 [英] Entity framework - select by multiple conditions in same column - referenced table

查看:141
本文介绍了实体框架 - 在同一列的多个条件选择 - 引用表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例方案:结果
两个表:订单 OrderItem的,关系一对多结果
我想选择这至少有一个OrderItem的价格与100和至少一个OrderItem的同价位200
所有的订单我可以做这样的:

Example scenario:
Two tables: order and orderItem, relationship One to Many.
I want to select all orders that have at least one orderItem with price 100 and at least one orderItem with price 200. I can do it like this:

var orders = (from o in kontextdbs.orders
              join oi in kontextdbs.order_item on o.id equals oi.order_id
              join oi2 in kontextdbs.order_item on o.id equals oi2.order_id
              where oi.price == 100 && oi2.price  == 200
              select o).Distinct();      



但是,如果这些条件得到用户产生的呢?
,所以我不知道有多少条件会有的。

But what if those conditions are user generated? So I dont know how many conditions there will be.

推荐答案

您需要通过所有值循环使用其中,任何的方法是这样的:

You need to loop through all the values using a Where and Any method like this:

List<int> values= new List() { 100, 200 };

var orders = from o in kontextdbs.orders
             select o;
foreach(int value in values)
{    
      int tmpValue = value;
      orders = orders.Where(x => kontextdbs.order_item.Where(oi => x.id == oi.order_id)
                                                      .Any(oi => oi.price == tmpValue));    
}

orders = orders.Distinct();

这篇关于实体框架 - 在同一列的多个条件选择 - 引用表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆