SQL 服务器查询到 C# Linq

SQL Server query to C# Linq

我目前在将我的 SQL 查询转换为 LINQ 用于我正在使用 WPF 和 Entity Framework

的学校项目时遇到了困难

这是我的 SQL 查询(完全符合我的预期)

select IngrediantName,sum(IngrediantQuantity) as Quantity, IngrediantMeasurementUnit from Users
join Shopping_List
on Users.UserID = Shopping_List.ShoppingListID
join List_Item
on List_Item.ShoppingListID = Shopping_List.ShoppingListID
join Ingrediant
on Ingrediant.IngrediantID = List_Item.IngrediantID
where Users.UserID = 1
group by IngrediantName,IngrediantMeasurementUnit

这是我目前的查询

var query = from user in dbContext.Users
                        join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
                        join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
                        join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
                        where currentUserNumber == user.UserId
                        
                        select new
                        {
                            name = ingrediant.IngrediantName,
                            quantity = ingrediant.IngrediantQuantity,
                            unit = ingrediant.IngrediantMeasurementUnit,
                        };

这是我目前尝试的方法

            var query = from user in dbContext.Users
                        join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
                        join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
                        join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
                        where currentUserNumber == user.UserId
                        group ingrediant by ingrediant.IngrediantQuantity into x
                        
                        select new
                        {
                            name = x.GroupBy(x => x.IngrediantName),
                            quantity = x.Sum(x => x.IngrediantQuantity),
                            unit = x.GroupBy(x => x.IngrediantMeasurementUnit),
                        };

这个return下面的错误说明不了什么

Argument type 'System.Linq.IQueryable1[System.Linq.IGrouping2[System.String,Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2[Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2

如果有人能指出正确的方向,我将不胜感激,如果您需要更多信息,我一定会提供

谢谢

更新

我在这里工作,为其他人提供问题的答案

var query = 
    from user in dbContext.Users
    join shoppingList in dbContext.ShoppingLists 
        on user.UserId equals shoppingList.UserId
    join listItem in dbContext.ListItems 
        on shoppingList.ShoppingListId equals listItem.ShoppingListId
    join ingrediant in dbContext.Ingrediants 
        on listItem.IngrediantId equals ingrediant.IngrediantId
    where currentUserNumber == user.UserId
    group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x
    select new
    {
        name = x.Key.name,
        quantity = x.Sum(x => x.IngrediantQuantity),
        unit = x.Key.unit,
    };

如果你看组线 group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x

根据我的理解,您使用 new 创建一个新的选择器,然后您可以使用 x.key.name 和 x.key.unit,其中名称和单位只是一些变量