2012-03-20 8 views
14

私は、次の表があります。このクロスクエリをLINQ-to-SQLに書き込む方法を教えてください。

create table TableA (
    Id int primary key identity, 
    Key int not null 
) 

create table TableB (
    Id int primary key identity, 
    TableA_Id int not null foreign key references TableA(Id), 
    Value varchar(80) not null 
) 

を私はラムダ表記を使用してLINQツーSQLで次のクエリを記述したいと思います:

select TableA.Key, b.Value 
from TableA 
cross apply (
    select top 10 TableB.Value 
    from TableB 
    where TableA.Id = TableB.TableA_Id 
    order by TableB.Value 
) b 
where TableA.Key between 0 and 999 

は、私はこれをどのように行うのでしょうか?

答えて

19

これはトリック

var query = from a in context.TableA 
      from b in context.TableB 
          .Where(x => x.TableA_Id == a.Id) 
          .OrderBy(x => x.Value) 
          .Take(10) 
      where a.Key >= 0 && a.Key <= 999 
      select new 
      { 
       a.Key, 
       b.Value, 
      }; 
1

//このコードはsytaxを適用外生成しますすべてのユーザー

var query = ActivityRepository.Where(p => p.iAction > -1 && 

    userIds.Contains(p.iSellerId)).GroupBy(c => c.iSellerId).Select(c => c.OrderByDescending(cc => cc.dBeginTime).First()).Select(a => new ActivityInfo 

     { 
      ActivityId = a.iActivityId, 
      StartTime = a.dBeginTime, 
      SellerId = a.iSellerId, 
      EndTime = a.dEndTime, 
      ActivityName = a.sName, 
     }); 

の最新の活動情報を取得すべきです。

関連する問題