[英]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/FirstOrDefault
, Single/SingleOrDefault
来获取该实例的单个实例。
statsModel.Donations = (from q in db.Shows where (q.Id == 1) select q.Donations)
.FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.