Question: Left outer join on 2 IQueryable<Object> objects
Hey, I want to know if it's possible to join 2 IQueryable<Object> objects with a LEFT OUTER join.
Let's start with an example:
var orderDetails = (IQueryable<Object>)context.Order_Details;
var products = (IQueryable<Object>)context.Products;
var joined = products.Join(orderDetails, "ProductID", "ProductID", "new(outer.ProductName as ProductName, inner.UnitPrice as Price)");
This works.
The reason I cast them to IQueryable<Object> is to simulate getting them during runtime by a string as an api call and getting them during reflection. To my knowledge, it's impossible to retain their 'true' Type.
How could I turn this into a left outer join instead?
All examples I saw had type knowledge during runtime.
@Blanen it's not clear what's your purpose: get names of all products with a price from order detail (or null if no details for this product exists) or get all order details (prices) with a product name (or null if product is not specified). Both seems rather strange to me but possible.
Assuming you need Products left join Order_Details (as in sql) then:
var orderDetails = (IQueryable)context.Order_Details;
var products = (IQueryable)context.Products;
var leftJoined = products
.GroupJoin(orderDetails, "ProductId", "ProductID", "new(outer as Product, inner as Order_Details)")
.SelectMany("Order_Details.DefaultIfEmpty()", "new(source.Product.ProductName, detail.UnitPrice as Price)", "source", "detail");
P.S: i'm not an expert, this works for me )
@isabekyan The purpose is not really relevant, as these are not real use cases. It's about the technical aspect with not having a the 'actual' Generic Type Argument during compile time. An example for either is ok.
Thanks for notifying me of the GroupJoin method anyway.
Hi, did you find a solution for having left outer join with dynamic linq?