Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use join with let in linq - c#

Tags:

c#

linq

let order= _relationContext.Orders 
             .Where(x => x.OrderNumber == orderNo)
             .Select(x => new { x.OrderNo, x.OrderDate }).Single()

I want to try and do something like this

let order = _relationContext.Orders join _relationContext.Products 
                     .Where(x => x.OrderNumber == orderNo && x.ProductId == Products.ProductID)
                     .Select(x => new { x.OrderNo, x.OrderDate }).Single()

Is this even possible?

UPDATE 1: My current code

    var q = from c in sXDocument.Descendants("prop")
        let handle = c.Element("handle")
        let resultref = handle != null ? handle.Element("dsref") : null
        let orderno = (string)c.Element("orderno")
        let orderdetail = _relationContext.Order 
                        .Where(x => x.orderno == orderno)
                        .Select(x => new { x.ProductID, x.OrderDate }).Single()

        select new Order()
        {
            OrderNo = orderno,
            Handle = resultref != null ? (string)resultref.Attribute("handle") : null,
            Title = //Need ProductName,

            ProductID = orderdetail.ProductID.ToString(),
        };

return q.ToList();

I figured that if I could use a join in let keyword so I could get the product name

like image 271
uno Avatar asked Apr 30 '10 19:04

uno


1 Answers

You can only use "let" as part of a query expression. If you want to define a separate variable, just declare it in the normal way. If you could give us more idea of what you're trying to do, that would really help.

EDIT: You can use "join" within a "let" clause, but only if it's a full query expression inside:

from foo in foos
let bar = (from x in baz join y in qux on x.Id equals y.Id select ...)
select ...

Are you sure you shouldn't be using a join instead of a let to start with?

like image 113
Jon Skeet Avatar answered Oct 27 '22 21:10

Jon Skeet