I have following two tables:
DocumentType
Id INT,
Name VARCHAR(100),
Active BIT,
CreatedBy INT
Document
Id INT,
DocumentTypeId INT,
Version SMALLINT,
Text NTEXT
I want to select DocumentType and related Document record with maximum value for Version. I tried following query:
from t in Documents
join tt in DocumentTypes on t.DocumentTypeId equals tt.Id
where tt.CreatedBy == 10
group t by t.DocumentTypeId into g
//let v = new {Version = g.Max( t => t.Version), TypeId =g.Key}
select new
{
Key = g.Key,
Version = g.Max(t=>t.Version),
Text = t.Text //ERROR AT t.Text
};
but it is giving me an error at following line:
Text = t.Text
The name 't' does not exist in the current context
I have tried g.Text also but it is not helping. Kindly help me to fix this query. I am trying this in LinqPad.
It seems that you need to retrieve a
Documententity withing the sameDocumentTypewhich has the max value for theVersionproperty. There is no need to group byntextcolumn.After grouping you have groups of documents. The only thing left is to get one with the max
Versionvalue for each group. I’d order the group by this property in descending order, and get the first value:You could project the result
Documententity into an anonymous type if you want.