1

我有一个显示供应商的成功查询,但我现在要添加的是显示已选择或未选择供应商的列(显示为复选框列)。这些选择存储在 VendorsSelected 表中,其中包含选择它们的 FK ProfileID 和 UserName。因此,当当前用户查看供应商时,将匹配某些供应商而不是其他供应商。

如何修改查询?请注意,下面的 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;
}

在此处输入图像描述

4

1 回答 1

2

向视图模型添加一个属性Selected( ):boolean

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;
}
于 2013-03-21T21:22:07.883 回答