SQL Server查询到C#LINQ

发布于 2025-01-19 17:47:19 字数 2749 浏览 0 评论 0原文

我目前有一个艰难的时间将我的SQL查询转换为LINQ,用于正在使用WPF的学校项目,

这是我的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),
                        };

这个错误

参数类型'system.linq.iqueryable 1 [system.linq.igrouping 2 [System.String,Microsoft.EntityFrameWorkcore.Query.Query.Query.TransparentIdentifierFactory+thransparentifierfactory+thransparentifentifier+code> Code> 2 [Microsoft.entityframeworkcore.query.query.query.query.query.query.query.query.query.query.query.query.query.query.query .TransparentIdentifierFactory+TransparentIdentifier2

如果有人可以将我指向正确的方向,我会很感激,如果您需要更多信息,我会确保提供

I'm currently having a rough time converting my SQL query to LINQ for a school project I'm using WPF and Entity Framework

here is my SQL query (working exactly as I expect)

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

Here is the query that I have so far

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,
                        };

Here is what I tried so far

            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),
                        };

this one return the following error which doesn't tell much

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

If someone could point me in the right direction I would appreciate it, if you need more info I will provide it for sure

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2025-01-26 17:47:19

更新

我在这里可以找到其他人问题的答案

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,
    };

如果您查看组行,
将 ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } 分组到 x

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

UPDATE

I got this working here the answers to the question for anyone else

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,
    };

if you look at the group line
group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x

from my understanding you use the new to create a new selector then you can use x.key.name and x.key.unit where name and unit are simply some variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文