6

I have next table:

MyTable
(
ParentId Integer,
Type Integer,
ProdId String,
Date DateTime,
Status Integer
);

I want to query as next:

var res = from tout in myTable.Where(t1 => t1.Type == 1)
                join tin in myTable.Where(t2 => t2.Type != 1)
        on tout.ParentId equals tin.ParentId
                where tout.ProdId == tin.ProdId && tout.Status > tin.Status
                orderby tout.Date
                select new MyTableStructure
                {
            ...
        };

How to write same as IQueryable using lambda?

4
  • 2
    Please correct me if I'm wrong, but from researching (b/c I didn't know how to do it either), it seems that both syntax look identical. However, it's the type that you're assigning to that makes the difference. (ie - IQueryable<MyTableStructure> res = ..... vs IEnumerable<MyTableStructure> res = ...... dotnettricks.com/learn/linq/ienumerable-vs-iqueryable Commented Mar 14, 2017 at 10:43
  • OK. Added that I also want to use lambda Commented Mar 14, 2017 at 11:42
  • msdn.microsoft.com/en-us/library/bb534675(v=vs.110).aspx Commented Mar 14, 2017 at 11:45
  • 1
    I recommend whenever using Joins in LINQ queries to always use the standard method instead of lambda. Reason being is that the Join syntax gets a little confusing (at least for me), and is easier read using the standard way. stackoverflow.com/questions/2767709/… Commented Mar 14, 2017 at 12:35

1 Answer 1

9

Something like this

var query1 = myTable.Where(t1 => t1.Type == 1);
var query2 = myTable.Where(t2 => t2.Type != 1);
var join = query1.Join(query2, x => x.ParentId, y => y.ParentId, (query1, query2) => new { query1 , query2 }).Where(o => o.query1.ProdId == o.qyuery2.prodId).......

your order by next and Something

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! The only note (it takes me some time to realize what's exactly wrong) - new { query2 , query1 } - wise-versa order should be there, to match with original join

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.