创建新的 Linq SelectMany 扩展方法

发布于 2024-09-10 16:28:54 字数 1366 浏览 7 评论 0原文

我正在使用 Linq.Dynamic。我已经向 all 添加了另一个 SelectMany 扩展,用于使用数据创建新的匿名对象。但是,我遇到了另一个我似乎无法解决的问题。

我想要按如下方式链接扩展方法,但使用动态方法:

var customerandorderflat = db.Customers
            .SelectMany(c => c.Orders.SelectMany(o => o.Order_Details,
                (ord, orddetail) => new
                        {
                            OrderID = ord.OrderID,
                            UnitPrice = orddetail.UnitPrice
                        }).DefaultIfEmpty(),
                (cus, ord) => new
                    {
                        CustomerId = cus.CustomerID,
                        CompanyName = cus.CompanyName,
                        OrderId = ord.OrderID == null ? -1 : ord.OrderID,
                        UnitPrice = ord.UnitPrice
                    });

理想情况下,我想按如下方式链接动态 SelectMany:

db.Customers.SelectMany(c => c.Orders.SelectMany("Order_Details", "new(outer.OrderID, inner.UnitPrice)"), "new(outer.CustomerID, inner.OrderID)");

或有影响的东西。问题是我无法获得匹配的签名。

我尝试了许多不同的选项来让它允许链接。但它就是行不通。我认为理想情况下它看起来像这样:

public static IQueryable SelectMany(this IQueryable source, IQueryable innerExpression, string resultsSelector, params object[] values)

但是,它不识别 c => c. 订单为 IQueriable。我还需要弄清楚如何对结果执行 DefaultIfEmpty 以允许 LEFT JOIN。

请帮忙。

I am using Linq.Dynamic. I have already added another SelectMany extension to all for creating a new anonymous object with the data. But, I have ran into another issue that I can not seem to solve.

I want to have extension method chaining as follows, but using the dynamic methods:

var customerandorderflat = db.Customers
            .SelectMany(c => c.Orders.SelectMany(o => o.Order_Details,
                (ord, orddetail) => new
                        {
                            OrderID = ord.OrderID,
                            UnitPrice = orddetail.UnitPrice
                        }).DefaultIfEmpty(),
                (cus, ord) => new
                    {
                        CustomerId = cus.CustomerID,
                        CompanyName = cus.CompanyName,
                        OrderId = ord.OrderID == null ? -1 : ord.OrderID,
                        UnitPrice = ord.UnitPrice
                    });

Ideally I would like to chain the dynamic SelectMany as follows:

db.Customers.SelectMany(c => c.Orders.SelectMany("Order_Details", "new(outer.OrderID, inner.UnitPrice)"), "new(outer.CustomerID, inner.OrderID)");

Or something to that affect. The problem is that I can not get a signature to match.

I have tried many different options to get it to allow chaining. But it just doesn't work. I am thinking ideally it would look like this:

public static IQueryable SelectMany(this IQueryable source, IQueryable innerExpression, string resultsSelector, params object[] values)

But, it doesn't recognize c => c.Orders as IQueriable. I also need to figure out how to do DefaultIfEmpty on the results to allow for LEFT JOINs.

Please help.

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

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

发布评论

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

评论(2

最初的梦 2024-09-17 16:28:54

c.Orders 是一个 EntitySet。 EntitySet 不实现 IQueryable。尝试c.Orders.AsQueryable()

c.Orders is an EntitySet. EntitySet doesn't implement IQueryable. Try c.Orders.AsQueryable()

花之痕靓丽 2024-09-17 16:28:54

它得到了错误的定义。正确定义时的实际错误:无法将 lambda 表达式转换为类型“System.Linq.IQueryable”,因为它不是委托类型

It was getting the wrong definition. Actual error when on right definition: Cannot convert lambda expression to type 'System.Linq.IQueryable' because it is not a delegate type

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