I have a successful query that returns multiple records from a joined table and also generates a bool Selected value (if a record exists for the current user in another table).
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;
}
I am trying to write another query that retreives one row but also needs to obtain that bool Selected value without projecting results into a class like the one above. This is my failed attempt to come close.
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;
}
This bool column I am trying to generate would be false if null (no records match) and true if a record is found. see this link if you need a visual of the model