Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ连接2个表_Linq_Join_Entity - Fatal编程技术网

LINQ连接2个表

LINQ连接2个表,linq,join,entity,Linq,Join,Entity,数据库中有两个表,其中一个表包含所有可能值的列表。比如说 Milk Cheese Bread Meat 第二个表包含从杂货店选择的项目。例如: Milk Cheese 我想要一个结果,所有可能的食品杂货项目与牛奶和奶酪的选择 有什么想法吗 这是桌子 The GroceryList Table: ID INT PK Description Varchar(50) The ShoppingList Table: ID INT PK GroceryListID int FK to Grocery

数据库中有两个表,其中一个表包含所有可能值的列表。比如说

Milk
Cheese
Bread
Meat
第二个表包含从杂货店选择的项目。例如:

Milk
Cheese
我想要一个结果,所有可能的食品杂货项目与牛奶和奶酪的选择

有什么想法吗

这是桌子

The GroceryList Table:
ID INT PK
Description Varchar(50)

The ShoppingList Table:
ID INT PK
GroceryListID int FK to GroceryList.ID
因此,生成的实体将是GroceryList中的所有项目,如果它们存在于ShoppingList中,则selected将标记为true: ShoppingList.ID 杂货店老板。描述
已选择编辑:听起来仍然像是要进行左连接。就你而言:

var LINQResult = from g in Datacontext.GroceryList
                 from s in DataContext.ShoppingList
                     .Where(c=>c.ID == g.ID)
                     .DefaultIfEmpty()
                 select new {
                    g.ID,
                    g.Description,
                    s.ID   // Will be null if not selected.
                  };
有关更多示例:


基于理解,您可以这样做

//first get the list of product which satisfy your condition

    var ids = (from p ShoppingList 
              select p.GroceryListID ).ToList();

//second filter the Grocery products by using contains

    var myProducts = from p in GroceryList 
                     where ids.Contains(p.ID)
                     Select p;

如果您想获得有关加入的信息,此图像将帮助您

内部联接

外部联接


试着理解,这可能有助于您解决查询

请发布您的表格(列)您的问题有点不清楚。。。你所说的“我想要的结果是所有可能的食品都有选择的牛奶和奶酪”是什么意思?你能给我们看一些样品吗?您是否只需要一个包含所有可能的食品杂货的列表,其中的属性表示是否选中了该商品?