我相信简单的Linq到实体查询涉及.Include

时间:2011-11-20 16:17:09

标签: sql linq-to-entities

我有一个Linq-to-Entities查询并不复杂但需要.include和/或投影和/或连接,因为它必须在一次传递中执行。

这是我的数据库(Microsoft SQL Server 2008):

表A(客户)(包含CustomerID(客户ID)和ZipCode(邮政编码)作为字符串。

表C(类别)(包含类别ID(类别),如“食物”,“庇护所”,“衣物”,“住房”(主键)。

表A_C是一个链接表,因为表A和表C链接为多对多:只包含两个字段:CustomerID“customer IDs”和CategoryID(Categories),组合为主键。该表是表A和表C之间的链接表。

这是我的查询,必须在一次数据库中执行:我需要选择表A中满足条件的所有记录,然后根据找到的“参数列表”筛选这些记录链接表A_C - 并在一次数据库中执行此操作。但我不知道表A_C的参数列表的长度或组成是提前的 - 它因呼叫而异。因此,这个参数列表通过方法调用来改变方法调用。

举一个更具体的例子:

表A列出了客户ID。我发现居住在某个邮政编码中的客户。然后,在同一个SQL查询中,我需要找到哪些客户选择了某些类别:食品,服装,住房等,但我的网络方法提前不知道这些类别是什么相反,它们作为列表传递给方法:列出myCategoryList(可以是1个类别或100个类别,并通过方法调用改变方法调用)。

如何使用Linq-to-Entities编写投影?当参数列表变化时?并且一次完成这一切?

  List<string> CategoryList = new List<string>() { "Food", "Shelter", "Housing" }; // in one call to the web service method

   List<string> CategoryList = new List<string>() { "Food", "Clothing" }; //could be a second call--it varies and I don't know ahead of time what the List will be

那么如何使用Linq-to-Entities进行SQL查询?一次通过? (当然,我可以遍历列表,并重复访问数据库,但这不是我告诉的最佳解决方案)。投射,。包括关键字,但网上冲浪没有产生任何结果。

这是一个粗略的猜测,只是为了让球滚动:

 public void WebMethod1 (CategoryList)
 {

 using (EntityFramework1 context = new EntityFramework1())

 {
  /* assume CategoryList is a list of strings passed into the method and is,for     this      particular call,something like:  List<string> CategoryList = new List<string>() { "Food", "Clothing" }; for this call,      but in the next call it could be: List<string> CategoryList = new List<string>() { "Food", "Shelter", "Housing" } */

 string ZipCodeString = "12345";
 string customerIDString = "E12RJ55";

 var CustomersFromZipCodeHavingSelectedCertainCategories =  from x in context.A_C
                            where x.A.CustomerID == customerIDString
                            where x.A.StartsWith(ZipCodeString)
                            where x.A_C.Contains(CategoryList) //???? This is clearly not grammatical, but what is?
                            select x;

 }

/ *

我的问题是:我想过滤来自A的所有包含邮政编码12345的记录,并且还有来自表A的某个CustomerID“E12RJ55”,但是进一步使用链接表A_C中的所有此类CustomerID过滤此集合“食品”和“服装”类别。

如何一次通过?我可以很容易地在多次传递和使用代码访问数据库时这样做,但是这个帖子中有人http://bit.ly/rEG2AM建议我做一个Join / projection并一次性完成所有这些。

* /

我也会接受SQL答案,因为它可能有助于产生解决方案。我相信这个问题并不困难 - 但我在网上找不到答案。

编辑:给大卫答案和信用。 我感谢你的回答david.s。这是有用的,与david.s的答案略有不同,因为我使用名为“Customer_Categories”的链接表(桥表),它位于表Customer和Categories之间,并包含每个主键(如需要)对于多对多的关系)。这个桥接表就是我在原来的答案中所谓的“A_C”,这里有一个整数而不是字符串但是是同一个东西。 Intellisense拿起这张桌子,我用它,它的工作原理。还要记住,CategoryList是一个int列表,List CategoryList = new List();但令人惊讶的是它在这个SQL-to-Entities查询中自动运行:

Var CustomersFromZipCOde = context.Customers.Where (custo => custo.CustomerID==customerIDString && custo.ZipCode.StartsWith(ZipCodeString) && custo.Customer_Categories.Any(categ => CategoryList.Contains(categ.CategoryID)));

//gives the right output, incredible.

1 个答案:

答案 0 :(得分:3)

首先我要说的是,即使你的解释很长,也不是很清楚。您想要一个简单的Linq-to-Entities查询,但是您没有给实体,您只谈论数据库中的表。

假设您有以下实体:

public class Customer
{
    public string CustomerID { get; set; }
    public string ZipCode { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
}

public class Category
{
    public string CategoryID { get; set; }
    public virtual ICollection<Customer> Customers { get; set; }
}

您的查询可能如下所示:

var CustomersFromZipCodeHavingSelectedCertainCategories =
    context.Customers.Where(
        customer => customer.CustomerID == customerIDString &&
                    customer.ZipCode.StartsWith(ZipCodeString) &&
                    customer.Categories.Any(
                        category => CategoryList.Contains(category.CategoryID));

有关其他方法的更多信息,请点击此处: http://smehrozalam.wordpress.com/2010/06/29/entity-framework-queries-involving-many-to-many-relationship-tables/