从另一个表中选择一个布尔列 [英] Selecting one bool column from another table

查看:134
本文介绍了从另一个表中选择一个布尔列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成功的查询,从一个连接的表返回多个记录,并且还生成一个bool Selected值(如果在另一个表中存在当前用户的记录)。

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

我正在尝试写另一个查询一行,但也需要获取这个bool选择的值不会将结果投射到类似上面的类中。这是我失败的尝试结束。

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

我试图生成的这个bool列将为false(否则为空)匹配),如果找到记录,则为true。请参阅此链接如果你需要一个视觉的模型

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

推荐答案

在这种情况下,因为我只想要一个额外的bool值显示在页面上因为这是一个编辑页面,其中视图模型必须映射回域模型...似乎像一个单一的布尔列的工作太多,所以我去了一个单独的查询,填充一个ViewBag :)

In this case since I only wanted one extra bool value to display on the page and as this was an Edit page in which the view model would have to be mapped back to the domain model... Seemed like too much work for a single bool column, so I went with a separate query that populates a ViewBag :)

这篇关于从另一个表中选择一个布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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