根据FK表中的记录返回列 [英] Return column based on record in FK table

查看:121
本文介绍了根据FK表中的记录返回列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a success query for displaying Vendors, but what I wanted to add now is a column showing which vendors have been selected or not (displayed as a checkbox column). These selections are stored in VendorsSelected table that contains the FK ProfileID and UserName that selected them. So when the current user views the Vendors there will be matches for some Vendors and not for others.

如何修改查询?请注意,下面的Where子句将完成获取当前用户所选择的供应商,但是我想要的是,所有供应商都显示了他们选择的每个供应商的true / false(checkbox)列。

How do I modify the query? Note the Where clause below will accomplish getting only vendors that the current user has selected, but what I want is all vendors displayed with true/false (checkbox) column for each vendor they have selected.

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;
}

推荐答案

添加一个属性 / code>( boolean )到视图模型:

Add a property Selected (boolean) to the view model:

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;
}

这篇关于根据FK表中的记录返回列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆