我有一个从连接表返回多条记录,并生成一个布尔值选择(如果记录存在另一个表中当前用户)查询成功。
public IEnumerable<BrowseVendorModel> SearchVendors(CustomSearchModel criteria)
{
var query = _db.VendorProfiles
.Include("VendorCategories")
.Include("VendorsSelected")
.Select(s => new BrowseVendorModel
{
ProfileID = s.ProfileID,
Name = s.Name,
CompanyName = s.CompanyName,
City = s.City,
State = s.State,
DateCreated = s.DateCreated,
// gets bool for selected vendors for current user
Selected = s.VendorsSelected.Select(vs => vs.UserName).Contains(HttpContext.Current.User.Identity.Name),
VendorsSelected = s.VendorsSelected,
VendorCategories = s.VendorCategories
})
.OrderBy(x => x.DateCreated);
return query;
}
我想写另一查询retreives一行还需要获得布尔值选择不突出成果转化为类似上面的类。 这是我失败的尝试接近。
public VendorProfile GetVendor(String id)
{
Guid pid = Guid.Parse(id);
var viewModel = _db.VendorProfiles
.Include("VendorCategories.ProductServiceCategory")
.Include("VendorsSelected")
.Select(s => new {
VendorProfiles = s,
Selected = s.VendorsSelected.Select(vs => vs.UserName).Contains(HttpContext.Current.User.Identity.Name)
})
.Where(s => s.VendorProfiles.ProfileID == pid);
return viewModel;
}
如果空(没有记录匹配),而真如果找到记录这布尔列我试图产生将是错误的。 看到这个链接 ,如果你需要一个可视化模型