5

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?

Ksice
  • 3,277
  • 9
  • 43
  • 67
  • 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 res = .....` vs `IEnumerable res = .....`. http://www.dotnettricks.com/learn/linq/ienumerable-vs-iqueryable – Rob Scott Mar 14 '17 at 10:43
  • OK. Added that I also want to use lambda – Ksice Mar 14 '17 at 11:42
  • https://msdn.microsoft.com/en-us/library/bb534675(v=vs.110).aspx – Milney Mar 14 '17 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. http://stackoverflow.com/questions/2767709/c-sharp-joins-where-with-linq-and-lambda – Rob Scott Mar 14 '17 at 12:35

1 Answers1

8

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

Vecchiasignora
  • 1,275
  • 7
  • 6
  • 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 – Ksice Mar 14 '17 at 14:35