Graham
Graham

Reputation: 47

Convert SQL statement to Linq-2-Sql

How do I translate the following SQL statement into L2S?

SELECT DefaultCode, MAX(EffectiveDt) AS EffectiveDt
FROM tblDF_DefaultSetting
GROUP BY DefaultCode

Upvotes: 2

Views: 119

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77546

You want to use the GroupBy operator on DefaultCode and use the Select operator to create a new anonymous type with the two values you're interested in.

dataContext.tblDF_DefaultSetting
    .GroupBy(x => x.DefaultCode)
    .Select(x => new { DefaultCode = x.Key, EffectiveDt = x.Max(x => x.EffectiveDt) });

Upvotes: 4

Related Questions