【问题标题】:Simple Linq-to-entities query involving .Include I believe涉及 .Include 我相信的简单 Linq-to-entities 查询
【发布时间】:2012-01-02 10:08:48
【问题描述】:

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

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

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

表 C(类别)(包含 CategoryID(类别),例如“food”、“shelter”、“clothing”、“housing”(主键)。

表 A_C 是一个链接表,因为表 A 和表 C 是多对多链接的:仅包含两个字段:CustomerID“客户 ID”和 CategoryID(类别),作为主键组合。此表是表 A 和表 C 之间的链接表。

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

举一个更具体的例子:

表 A 有一个客户 ID 列表。我找到住在某个邮政编码中的客户。然后,在同一个 SQL 查询中,我需要找到这些客户中的哪些选择了某些类别:食品、服装、住房等,但我的 web 方法不提前知道这些类别是什么,而是将它们作为列表传递给方法:List 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 查询呢?一口气? (当然我可以遍历列表,并重复访问数据库,但这不是我被告知的最佳解决方案)。 Projection,.Include 是关键字,但上网一无所获。

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

 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 建议我做一个加入/投影并一举完成。

*/

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

编辑:感谢 david s。 我感谢您的回答 david.s。这是有效的方法,与 david.s 的答案略有不同,因为我使用的是名为“Customer_Categories”的链接表(桥表),它位于表 Customer 和 Categories 之间,并且包含每个表的主键(根据需要对于多对多关系)。这个桥接表在我原来的答案中就是我所说的“A_C”,这里有整数而不是字符串,但是是一样的。 Intellisense 拿起了这张桌子,我使用了它,它可以工作。还要记住,CategoryList 是一个整数列表,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.

【问题讨论】:

    标签: sql linq-to-entities


    【解决方案1】:

    首先我想说的是,即使你的解释很长,也不是很清楚。您想要一个简单的 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/

    【讨论】:

    • 谢谢。我会试试这个例子和链接。我没有明确的类,服务器端。我只是在 VS10 中选择从我的数据库生成 .edmx 图。因此我没有“public virtual Icollection...”这有关系吗?我认为不会——我将依赖 IntelliSense。您的示例中感兴趣的一件事是这个 CategoryList.Contains(category.CategoryID)。如果这行得通,我会感到非常惊讶,因为 CategoryList 是一个值列表。另一方面,您引用的链接使用类似于二维数组的东西,所以也许可以吗?
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多