MVC 5无法将类型'System.Linq.IQueryable隐式转换为bool吗? [英] MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool?
问题描述
在我生命中,我无法弄清楚如何得到错误Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'bool?'
,如果我双击错误会将其带到HomeController并突出显示statsModel.Donations = (from q in db.Shows where (q.Id == 1) select new { q.Donations });
行中的select语句.
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?'
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 });
.
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
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);
}
任何人都可以提供帮助,我要返回的是Show表中的Donations和People字段对于特定记录是正确还是错误.
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.
任何帮助我都会很感激:-)
Any help would me much appreciated :-)
推荐答案
假定Donations
是模型中的布尔字段,并且您只希望有一条记录用于tat ID,我想您只是想要:
Assuming that Donations
is a boolean field in your model and you only expect one record for tat ID I think you just want:
statsModel.Donations = (from q in db.Shows where (q.Id == 1) select q.Donations).Single();
或
statsModel.Donations = db.Shows.Where(q => q.Id == 1).Single(q => q.Donations);
这篇关于MVC 5无法将类型'System.Linq.IQueryable隐式转换为bool吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!