I want to write the following SQL as a Linq to Entities statement
select min(enddate), Code
from t1
inner join t2
on t1.t2id = t2.id
group by Code
What I have so far is
var data = ctx.t1.Join(ctx.t2,
one => one.t2id,
two => two.Id,
(one, two) => new {one, two})
.GroupBy(onetwo => onetwo.one.t2id)
.Min(onetwo => WHAT GOES HERE?);
I think the only thing I am missing is what goes in WHAT GOES HERE?
. I could be wrong and have gone totally astray but as far as I know this is the only thing I am missing. Any idea what I can do?
What you should be using is a Select
not a Min
, otherwise you will only be able to return a single value. For example, something like this:
var data = ctx.t1
.Join(ctx.t2, one => one.t2id, two => two.Id, (one, two) => new {one, two})
.GroupBy(onetwo => onetwo.one.t2id)
.Select(x => new
{
Code = x.Key,
MinDate = x.Min(g => g.one.EndDate) //This may need to be g.two.EndDate
});