1

メインの VendorProfile テーブルと、ステータス コードと日付スタンプを含む 1 対多の VendorHistory テーブルがあります。以下のクエリは、各ベンダーの最新のステータス (ステータス コードと日付) のみを取得するために機能します。ただし、私がやりたいことは、ステータス コードを StatusCodes ルックアップ テーブルにあるステータス名に変換することです。

モデル図

public IEnumerable<BrowseStatusModel> BrowseByStatus()
{
    IQueryable<BrowseStatusModel> viewModel = _db.VendorProfiles
    .Include("VendorStatusHistory")
    .Include("StatusCodes")
    .Select(s => new BrowseStatusModel
    {
        ProfileID = s.ProfileID,
        Name = s.Name,
        CompanyName = s.CompanyName,
        DateCreated = s.DateCreated,
        Status = s.VendorStatusHistories.OrderByDescending(o => o.DateCreated).FirstOrDefault().Status, 
        StatusDate = s.VendorStatusHistories.OrderByDescending(o => o.DateCreated).FirstOrDefault().DateCreated,
        StatusName = ???
    })
    .OrderBy(x => x.ProfileID);
    return viewModel;
}

おまけのフォローアップとして、VendorHistory テーブルに一致する行があるベンダーのみを返すように上記のクエリを変更して、履歴レコードを持たないレコードを除外する方法を知りたいです。

4

1 に答える 1

1

最初に最後のものを選択し、VendorStatusHistory次にそのプロパティを選択できます。

_db.VendorProfiles
.Select(s => new { 
                   Profile = s, 
                   LastHist = s.VendorStatusHistories
                               .OrderByDescending(o => o.DateCreated)
                               .FirstOrDefault()
                 })
    .Select(x => new BrowseStatusModel
    {
        ProfileID = x.Profile.ProfileID,
        Name = x.Profile.Name,
        CompanyName = x.Profile.CompanyName,
        DateCreated = x.Profile.DateCreated,
        Status = x.LastHist.Status, 
        StatusDate = x.LastHist.DateCreated,
        StatusName = x.LastHist.StatusCOde.StatusName
    })
    .OrderBy(x => x.ProfileID);
于 2013-04-24T19:06:55.197 に答える