开发者

Create new Linq SelectMany extension method

开发者 https://www.devze.com 2023-01-08 17:11 出处:网络
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 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 sourc开发者_开发问答e, 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消