简体   繁体   English

MVC 5无法将类型'System.Linq.IQueryable隐式转换为bool吗?

[英]MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool?

For the life of me I cannot figure how I am getting the error Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'bool?' 对于我一生,我无法弄清楚如何得到错误Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'bool?' if I double-click the error it takes me to the HomeController and highlights the select statement within the line statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations }); 如果我双击该错误,它将带我到HomeController并突出显示statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations });行中的select语句statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations }); .

Show.cs Show.cs

namespace WebApplication1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Show
    {
        public int Id { get; set; }
        public Nullable<bool> Donations { get; set; }
    }
}

StatsisticsModel.cs StatsisticsModel.cs

namespace WebApplication1.Models
{
    using System;

    public class StatisticsModels
    {
        public int UserCount { get; set; }
        public decimal? TotalAmount { get; set; }
        public Nullable<bool> Donations { get; set; }
    }
}

HomeController 家庭控制器

[ChildActionOnly]
public ActionResult Statistics()
{
    var statsModel = new StatisticsModels();
    statsModel.UserCount = (from m in db.AspNetUsers where (m.UserName != "someone") && (m.EmailConfirmed == true) select new { m.UserName }).Count();
    statsModel.TotalAmount = db.Donations.Sum(o => o.Amount);
    statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations });
    return View(statsModel);
}

Can anyone please help, all I want to return is whether the Donations and People fields within the Show table are true or false for a particular record. 任何人都可以帮忙,我想返回的是Show表中的Donations和People字段对于特定记录是正确还是错误。

Any help would me much appreciated :-) 任何帮助,我将不胜感激:-)

Assuming that Donations is a boolean field in your model and you only expect one record for tat ID I think you just want: 假设Donations是模型中的一个布尔值字段,并且您只希望为tat ID记录一条记录,我想您只想要:

statsModel.Donations = (from q in db.Shows where (q.Id == 1) select q.Donations).Single();

or 要么

statsModel.Donations = db.Shows.Where(q => q.Id == 1).Single(q => q.Donations);

Problem is with this line: 问题是这条线:

 statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations });

This will try to assign the a collection of anonymous type object to Donations which is can hold a single value (Nullable). 这将尝试将匿名类型对象的集合分配给Donations ,该对象可以包含单个值(Nullable)。

all I want to return is whether the Donations and People fields within the Show table are true or false for a particular record. 我要返回的只是对于特定记录,Show表中的Donations和People字段是true还是false。

You need something like: 您需要类似:

statsModel.Donations = db.Shows.Any(q=> q.Id == 1);

If your field Donations in database is of type boolean, then you need to get a single instance of that from your query, using either First/FirstOrDefault , Single/SingleOrDefault depending on your requirement. 如果您的数据库中的字段Donations类型为布尔类型,则需要根据需要从查询中使用First/FirstOrDefaultSingle/SingleOrDefault来获取该实例的单个实例。

statsModel.Donations = (from q in db.Shows where (q.Id == 1) select q.Donations)
                       .FirstOrDefault();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换类型&#39;System.Linq.IQueryable <Database.Table> &#39;到&#39;布尔&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<Database.Table>' to 'bool' 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable &lt;&gt;&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>' 无法将类型'System.Linq.IQueryable <int>'隐式转换为'int?' - Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?' 无法隐式转换System.Linq.IQueryable类型 <string> 串起来 - Cannot implicitly convert type System.Linq.IQueryable<string> to string 无法隐式转换类型&#39;System.Linq.IQueryable? - Cannot implicitly convert type 'System.Linq.IQueryable? 无法隐式转换类型'System.Linq.IQueryable <AnonymousType#1>' - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' 无法隐式转换类型&#39;System.Linq.IQueryable <double> &#39;到&#39;加倍&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<double>' to 'double' 无法隐式转换类型&#39;System.Linq.IQueryable - Cannot implicitly convert type 'System.Linq.IQueryable 无法在内部联接上隐式转换类型system.linq.iqueryable - cannot implicitly convert type system.linq.iqueryable on inner join 无法隐式转换类型&#39;System.Linq.IQueryable <Model> &#39; 模拟&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<Model>' to 'Model'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM