2011-11-20 30 views
0

我有一個Linq-to-Entities查詢不復雜,但需要一個.include和/或投影和/或連接,因爲它必須一次執行。簡單的Linq到實體查詢涉及.Include我相信

這裏是我的數據庫(微軟SQL Server 2008中):

表A(客戶)(包含客戶ID(客戶ID)和郵編(郵政編碼)爲字符串

表C(類別) (包含「食物」,「住所」,「服裝」,「住房」(主鍵)等CategoryID(類別))。許多:只包含兩個字段:客戶ID「客戶ID」和類別ID(類別),組合爲主鍵。該表是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建議我做一個加入/投影,並一舉完成。

*/

我也接受SQL的答案,因爲它可能有助於產生一個解決方案。這個問題btw並不難,我相信 - 但我無法在網上找到答案。

編輯:與答案和信貸給大衛s。 我感謝你的答案david.s。這是什麼工作,與david.s的答案稍有不同,因爲我使用Customer Customer和Categories表之間的名爲「Customer_Categories」的鏈接表(橋表),並且包含每個表的主鍵(根據需要對於多對多的關係)。這個橋表是我原來的答案中所謂的「A_C」,這裏有整數而不是字符串,但是是一樣的。 Intellisense拿起了這張桌子,我用它,它的工作原理。也請記住,所屬分類爲整數的列表,列表所屬分類=新名單();,但令人驚訝的是自動地運作這個SQL到實體查詢中:

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. 

回答

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/

+0

感謝。我會嘗試這個例子和鏈接。我沒有明確的類,服務器端。我簡單地在VS10中選擇了從我的數據庫生成.edmx圖。因此,我沒有「公共虛擬Icollection ...」這是否重要?我想不是 - 我將依靠智能感知。在你的例子中感興趣的一件事是這個CategoryList.Contains(category.CategoryID)。如果這個工作,我會非常驚訝,因爲CategoryList是一個值列表。另一方面,你引用的鏈接使用類似於二維數組的東西,所以也許它可以嗎? – PaulDecember