我有一个“Linq-to-Entities”的质问,这并不复杂,但需要一.include和(或)投射和(或)加入,因为必须用一个通行证执行。
这里是我的数据库(MicroSoft服务器2008年):
表A(客户)(客户信息(客户身份)和ZipCode(编码))为示意图。
表C(城市)(包括“食品”、“住房”、“住房”、“住房”(主要钥匙)。
Table A_C is a linking table, since Tables A and C are linked as many-to-many: contains just two fields: CustomerID "customer IDs" and CategoryID (Categories), in combination as primary keys. This table is a linking table betweeen tables A and C.
我在此询问,这必须在访问数据库时进行: 我需要选择表A中符合条件的所有记录,然后根据连接表A_C-的参数清单对这些记录进行过滤,并在一次访问数据库时这样做。 但我不知道表A_C的参数清单的长短或组成,在时间要求不同之前,时间不同。 因此,这一参数清单按方法要求有所不同。
To give a more concrete example:
表A列有客户身份清单。 我发现客户生活在某种Zip法典中。 然后,在同一个问询栏<>中,我需要找到其中哪些客户选择了某些类别: 粮食 衣物、住房等,但我的网络方法并不事先知道这些类别是什么,而是将它们作为方法的列表: 列出我的语言学家(可分为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
因此,我如何利用Lig-to-Entities来做QQ? 有一个通行证? (当然,我可以选择名单,并多次访问数据库,但这并非我所说的最佳解决办法。) 预测:关键词是关键词,但超过净值则无结果。
这里是一只粗gues的gues子,只是为了滚滚滚动:
public void WebMethod1 (CategoryList)
{
using (EntityFramework1 context = new EntityFramework1())
{
页: 1 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;
}
页: 1
我的问题是:我想从A中抽取所有记录,这些记录中含有第12345号手法,在表A中还有某些客户信息数据库“E12RJ55”,但又把这一记录与所有此类客户信息数据库相连接,将表A-C中包含“食物”和“食物”类别。
如何在一份通行证中做到这一点? 我可以在使用密码的多张通行证和访问数据库时非常容易地这样做,但有人在座右边。 我建议我做的是Join/projection,在一刀切的情况下这样做。
*
我也接受卡片的答复,因为这可能有助于找到解决办法。 这个问题不难解决,但我认为我无法找到对这个净额的答案。
EDIT: with answer and credit to david s. I thank you for the answer david.s. Here is what worked, slightly different than the answer by david.s, in that I am using the linking table (bridge table) called “Customer_Categories” that is between the table Customer and Categories and contains the primary key of each (as is required for many-to-many relationships). This bridge table is what I called "A_C" in my original answer, and here has ints rather than strings but is the same thing. Intellisense picked up this table and I used it, and it works. Also keep in mind that CategoryList is a list of ints, List CategoryList = new List();, yet amazingly it automagically works inside this SQL-to-Entities query:
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.