1

次の2つのテーブルがあります。

DocumentType
    Id INT,
    Name VARCHAR(100),
    Active BIT,
    CreatedBy INT

Document
    Id INT,
    DocumentTypeId INT,
    Version SMALLINT,
    Text NTEXT

の最大値を持つDocumentType関連レコードを選択したい。次のクエリを試しました:DocumentVersion

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
       };

しかし、次の行でエラーが発生しています:

Text = t.Text

The name 't' does not exist in the current context

私も試しg.Textましたが、役に立ちません。このクエリを修正するのを手伝ってください。LinqPadでこれを試しています。

4

3 に答える 3

2

プロパティの最大値を持つDocumentエンティティを取得する必要があるようです。列ごとにグループ化する必要はありません。DocumentTypeVersionntext

グループ化すると、ドキュメントのグループができます。残っている唯一のことはVersion、各グループの最大値を持つものを取得することです。このプロパティでグループを降順で並べ替え、最初の値を取得します。

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
select g.OrderByDescending(t => t.Version).FirstOrDefault();

必要に応じて、結果Documentエンティティを匿名型に射影できます。

于 2013-02-21T11:20:19.833 に答える
1

試す

from t in Documents 
join tt in DocumentTypes on  t.DocumentTypeId equals tt.Id 
where tt.CreatedBy == 10
orderby t.Version descending
group t by t.DocumentTypeId into g

select new 
    {
        Key      = g.Key, 
        Version  =  g.First().Version, 
        Text     =  g.First().Text 
    };
于 2013-02-21T19:19:15.750 に答える
0

tすでに何か他のものを表しています。
この方法を試してください

Version = g.Max(x=>t.Version), 
于 2013-02-21T05:30:09.803 に答える