Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
C# LINQtoSQL根据另一个表的值检索一个表,该表与_C#_Linq_Linq To Sql - Fatal编程技术网

C# LINQtoSQL根据另一个表的值检索一个表,该表与

C# LINQtoSQL根据另一个表的值检索一个表,该表与,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有以下三个表格: 使用LinqToSql,我想检索一个清单项目列表,其中(pointsName=“Level”和pointsValue类似于: var items = inventoryItems.Where( invItem => invItem.InventoryItemPoints.Contains( iip => (iip.pointsName == "Buy Price" || iip.PointsName == "Level") &&

我有以下三个表格:

使用LinqToSql,我想检索一个清单项目列表,其中(pointsName=“Level”和pointsValue类似于:

var items = inventoryItems.Where(
  invItem => invItem.InventoryItemPoints.Contains(
    iip => (iip.pointsName == "Buy Price" || iip.PointsName == "Level") &&
    iip.pointsValue == maxStoreLevel);
但根据您发布的内容,这只是一个猜测,如果不是这样的话,dbml的屏幕将非常有用;)

更新:好的-这显然是垃圾(对不起,两杯酒太多了:)

试试这个:

var items = dataContext.inventoryItems.Where(
    invItem => ivnItem.InventoryItemPoints.Select(
        iip => iip.Point.PointName).Contains(
        "Buy Price")
        && invItem.InventoryItemPoints.Select(
            iip => iip.pointsValue).Contains(maxStoreLevel));
添加其他点名称应该很容易…

类似于:

var items = inventoryItems.Where(
  invItem => invItem.InventoryItemPoints.Contains(
    iip => (iip.pointsName == "Buy Price" || iip.PointsName == "Level") &&
    iip.pointsValue == maxStoreLevel);
但根据您发布的内容,这只是一个猜测,如果不是这样的话,dbml的屏幕将非常有用;)

更新:好的-这显然是垃圾(对不起,两杯酒太多了:)

试试这个:

var items = dataContext.inventoryItems.Where(
    invItem => ivnItem.InventoryItemPoints.Select(
        iip => iip.Point.PointName).Contains(
        "Buy Price")
        && invItem.InventoryItemPoints.Select(
            iip => iip.pointsValue).Contains(maxStoreLevel));

添加其他点名称应该很容易…

好的,所以这没有lambda表达式那么漂亮,我仍然不清楚where子句到底应该是什么,因为PointsName不能同时是Level和Buy Price,但我需要在某个地方开始对话。我猜你需要在points表上进行2次连接,但是因为你比我更了解你的设置,我猜你可以根据需要进行修改。让我知道我错过了什么

var items = (From items in context.InventoryItems
             join itemPoints in context.InventoryItemPoints on items.InventoryItemID equals itemPoints.InventoryItemID
             join points in context.Points on itemPoints.pointsID equals points.pointsID
             where (points.pointsName == "Level" && itemPoints.pointsValue == maxStoreLevel) && points.pointsName == "Buy Price"
             select items).Distinct();
我知道原始文件不会返回行,因为点名称无法同时包含这两个值,但基于后续更新,我认为您需要的是:

var items = (From items in context.InventoryItems
             join levelItemPoints in context.InventoryItemPoints on items.InventoryItemID equals levelItemPoints.InventoryItemID
             join levelPoints in context.Points on levelItemPoints.pointsID equals levelPoints.pointsID
             join priceItemPoints in context.InventoryItemPoints on items.InventoryItemID equals priceItemPoints.InventoryItemID
             join pricePoints in context.Points on priceItemPoints.pointsID equals pricePoints.pointsID
             where (levelPoints.pointsName == "Level" && levelItemPoints.pointsValue == maxStoreLevel) && pricePoints.pointsName == "Buy Price"
             select items).Distinct();

好吧,这不像lambda表达式那么漂亮,我仍然有点不清楚where子句到底应该是什么,因为PointsName不能同时是Level和Buy Price,但我需要在某个地方开始对话。我猜你需要在points表上进行2次连接,但是因为你比我更了解你的设置,我猜你可以根据需要进行修改。让我知道我错过了什么

var items = (From items in context.InventoryItems
             join itemPoints in context.InventoryItemPoints on items.InventoryItemID equals itemPoints.InventoryItemID
             join points in context.Points on itemPoints.pointsID equals points.pointsID
             where (points.pointsName == "Level" && itemPoints.pointsValue == maxStoreLevel) && points.pointsName == "Buy Price"
             select items).Distinct();
我知道原始文件不会返回行,因为点名称无法同时包含这两个值,但基于后续更新,我认为您需要的是:

var items = (From items in context.InventoryItems
             join levelItemPoints in context.InventoryItemPoints on items.InventoryItemID equals levelItemPoints.InventoryItemID
             join levelPoints in context.Points on levelItemPoints.pointsID equals levelPoints.pointsID
             join priceItemPoints in context.InventoryItemPoints on items.InventoryItemID equals priceItemPoints.InventoryItemID
             join pricePoints in context.Points on priceItemPoints.pointsID equals pricePoints.pointsID
             where (levelPoints.pointsName == "Level" && levelItemPoints.pointsValue == maxStoreLevel) && pricePoints.pointsName == "Buy Price"
             select items).Distinct();

虽然不能将where子句加倍,但可以使用sub selects和Count查看它们是否包含值。尝试:

var items = from item in context.InventoryItems
    where (from iip in context.InventoryItemPoints
        join p in context.Points on p.pointsId equals iip.pointsId
        where iip.InventoryItemId == item.InventoryItemId 
                    && p.pointsName == "Level" && iip.pointsValue == maxStoreLevel
        select iip).Count() > 0
    && (from iip in context.InventoryItemPoints
        join p in context.Points on p.pointsId equals iip.pointsId
        where iip.InventoryItemId == item.InventoryItemId 
                    && p.pointsName == "Buy Price"
        select iip).Count() > 0
    select item;

虽然不能将where子句加倍,但可以使用sub selects和Count查看它们是否包含值。尝试:

var items = from item in context.InventoryItems
    where (from iip in context.InventoryItemPoints
        join p in context.Points on p.pointsId equals iip.pointsId
        where iip.InventoryItemId == item.InventoryItemId 
                    && p.pointsName == "Level" && iip.pointsValue == maxStoreLevel
        select iip).Count() > 0
    && (from iip in context.InventoryItemPoints
        join p in context.Points on p.pointsId equals iip.pointsId
        where iip.InventoryItemId == item.InventoryItemId 
                    && p.pointsName == "Buy Price"
        select iip).Count() > 0
    select item;

是否定义了外键?你能发布dbml图吗?我正在做一个例子,但我需要一些关于标准的说明……你可以说“where pointsName=“Level”、pointsName=“Buy Price”和pointsValue=maxStoreLevel”。这是(pointsName=“Level”或pointsName=“Buy Price”)和pointsValue=maxStoreLevel…还是pointsName=“Level”或(pointsName=“Buy Price”和pointsValue=maxStoreLevel)。此外,在maxStoreLevel上,点中是否只有一行具有pointsName=“Level”或每个InventoryItem一行?我们可以假设每个InventoryItem都有一个具有该值的点吗?是否定义了外键?你能发布dbml图吗?我正在做一个例子,但我需要一些关于标准的说明……你可以说“where pointsName=“Level”、pointsName=“Buy Price”和pointsValue=maxStoreLevel”。这是(pointsName=“Level”或pointsName=“Buy Price”)和pointsValue=maxStoreLevel…还是pointsName=“Level”或(pointsName=“Buy Price”和pointsValue=maxStoreLevel)。此外,在maxStoreLevel上,点中是否只有一行具有pointsName=“Level”或每个InventoryItem一行?我们可以假设每个InventoryItem都有一个具有该值的点吗?表示它无法将lambda表达式转换为InventoryItemPoint类型,因为它不是一个delegateYep,这是垃圾,我从来都不喜欢LINQ to SQL版本的SQL“IN”…尝试过这样做:var InventoryItems=ydc.InventoryItems.Where(invItem=>invItem.InventoryItemPoints.Select(iip=>iip.Points.pointsName)。包含(“购买价格”)&&(invItem.InventoryItemPoints.Select(iip=>iip.pointsValue)。包含((int)maxStoreLevel)和&invItem.InventoryItemPoints.Select(iip=>iip.Point.pointsName).包含(“级别”));但未返回任何行:/Says它无法将lambda表达式转换为InventoryItemPoint类型,因为它不是一个delegateYep,这是垃圾,我对LINQ to SQL版本的SQL“IN”…从来都不满意。尝试了以下方法:var InventoryItems=ydc.InventoryItems.Where(invItem=>invItem.InventoryItemPoints.Select(iip=>iip.Points.pointsName)。包含(“购买价格”)&&(invItem.InventoryItemPoints.Select(iip=>iip.pointsValue)。包含((int)maxStoreLevel)和&invItem.InventoryItemPoints.Select(iip=>iip.Point.pointsName).包含(“级别”));但未返回任何行:/It返回了InventoryItems表中的所有行。我认为这是一个不正确的结果?这是一个复杂的查询,但这个概念应该可以工作。我承认,我可能需要在调试器中对其进行一点调试才能完全解决。它返回了InventoryItems表中的所有行。我认为这是一个不正确的结果t?这是一个复杂的查询,但是这个概念应该会起作用。我承认我可能需要在调试器中对它进行一点调试才能完全解决它。按照您所说的那样工作,谢谢您的帮助和耐心。正如您所说,谢谢您的帮助和耐心