1

我有顯示供應商成功的查詢,但我想現在補充的是哪家廠商已經選擇或者不列顯示(顯示爲一個複選框列)。這些選擇都存儲在包含FK簡檔,並且選擇他們用戶名VendorsSelected表。因此,噹噹前用戶查看供應商時,一些供應商會匹配而不是其他供應商。返回基於記錄列在表FK

如何修改查詢?注意:Where子句下面將完成只得到供應商,目前用戶已經選擇了,但我要的是與真/假(複選框)柱子上他們已經選擇了每個廠商中顯示的所有供應商。

public IEnumerable<BrowseVendorModel> BrowseVendors() 
{ 
    IQueryable<BrowseVendorModel> viewModel = _db.VendorProfiles 
     .Include("VendorsSelected") 
     .Select(s => new BrowseVendorModel 
     { 
      ProfileID = s.ProfileID, 
      Name = s.Name, 
      CompanyName = s.CompanyName, 
      City = s.City, 
      State = s.State, 
      DateCreated = s.DateCreated 
     }) 
     .Where(x => x.VendorsSelected.Select(s => s.UserName).Contains(HttpContext.Current.User.Identity.Name)) 
     .OrderBy(v => v.ProfileID); 

    return viewModel; 
} 

enter image description here

回答

2

屬性Selectedboolean)添加到瀏覽模式:

public IEnumerable<BrowseVendorModel> BrowseVendors() 
{ 
    IQueryable<BrowseVendorModel> viewModel = _db.VendorProfiles 
     .Include("VendorsSelected") 
     .Select(s => new BrowseVendorModel 
     { 
      ProfileID = s.ProfileID, 
      Name = s.Name, 
      CompanyName = s.CompanyName, 
      City = s.City, 
      State = s.State, 
      DateCreated = s.DateCreated, 
      Selected = x.VendorsSelected.Select(s => s.UserName) 
         .Contains(HttpContext.Current.User.Identity.Name) 
     }) 
     .OrderBy(v => v.ProfileID); 

    return viewModel; 
} 
+0

OMG,我不知道你能做到這一點。我花了幾年時間寫SQL,但不得不重新學習LINQ方法。我要去哪裏學習像你這樣的LINQ忍者? :) THX – SQLGrinder 2013-03-21 22:32:19

+0

只要繼續做我猜:) – 2013-03-21 22:37:53

+0

發佈另一個LINQ忍者[問題](http://stackoverflow.com/questions/15671371/linq-to-entities-selecting-one-bool-column-from-另一個表)修改上述查詢。 – SQLGrinder 2013-03-27 23:35:11