【问题标题】:Error in linq query when using average使用平均值时 linq 查询出错
【发布时间】:2013-09-25 16:06:31
【问题描述】:

我的数据库中有两个表,picturepictureratings

public partial class picture
{
    public int idpicture { get; set; }
    public int iduser { get; set; }
    public string picTitle { get; set; }
    public string picFilename { get; set; }
    public System.DateTime pictime { get; set; }
    public int nuditylevel { get; set; }
    public int fakeslevel { get; set; }

    // This property will hold the total accumulated/summed 
    // up rating of a picture
    public int totalrating { get; set; }    
}

public partial class pictureratings 
{
    public int idrating { get; set; }
    public int idpictures { get; set; }
    public int iduser { get; set; }
    public System.DateTime iddatetime { get; set; }
    public int iduserrateddby { get; set; }
    public int rating { get; set; } 
}

对于每个评分,都会创建一个新的pictureratings 行。我想按图片 ID 对 pictureratings 表进行分组,然后计算点赞数。我想在totalrating 属性的picture 表中显示这些喜欢。

所以按照我现在的情况,我可以编写以下代码

 var combo = from p in db.picturedetails
                    join l in db.picturelikes on p.idpictures equals l.idpictures into pl
                    select new LikesandPictureDetails
                    {
                             IdUserPic = p.iduser,
                           IdPicturess =p.idpictures,
                           Likes = p.likes,
                           NudityLevel = p.nuditylevel,
                           PicTitle = p.picTitle,
                           PicTime = p.pictime,
                           picFilename=p.picFilename,
                           Count=pl.Count(),
                           totalrating = pl.Average(c => c.likenumber) // This line is causing error
                    };

我正在使用 web api 返回查询总数。我正在显示picture 属性,例如iduserpicTitlepicFilenamepictimenuditylevelfakeslevel

按照现在,每件事都运行顺利,但是当我添加 totalrating = pl.Average(c => c.likenumber) 时,我得到一个异常说

转换为值类型“Double”失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可为空的类型。

如何消除错误?

【问题讨论】:

标签: c# sql asp.net-mvc linq lambda


【解决方案1】:

可能问题是pl没有记录,试试

替换

totalrating = pl.Average(c => c.likenumber) // This line is causing error

totalrating = pl.DefaulIfEmpty(0).Average(c => c.likenumber) 

【讨论】:

    【解决方案2】:

    db.picturelikes.likenumber 似乎可以为空?但是找不到代码中定义的属性。 无论如何,您可以通过放置

    来选择所有不为空的条目
    pl.Where(a=>a.likenumber != DBNull.Value /* or null whatever this is... */).Average...
    

    如果是可为空的属性:

    pl.Where(a=>a.likenumber.HasValue).Average(a=>a.likenumber.Value)
    

    :编辑 我认为 .average 在这种情况下返回一个可以为空的小数。要将其分配给您的字段,只需输入

    totalrating = pl.Average(c => c.likenumber) 
    

    Totalrating 似乎是一个 int,或者您将通过将其转换为 int 来丢失一些信息,或者您将类型更改为 double...

    【讨论】:

    • 两者都不起作用。 Hasvalue 选项在智能感知中没有出现。
    • likenumber是什么样的类型?
    • Dbnull 给出 Errr : 运算符 '!=' 不能应用于 'int' 和 'System.DBNull' 类型的操作数
    • 啊,.Average 返回可为空的小数。简单地说.Value, pl.Average(c => c.likenumber).Value
    【解决方案3】:

    错误似乎是您的数据库有一列可以包含null 值,但您的对象具有不可为空的double 类型。

    如果您选择匿名类型而不是具体类型,查询是否会运行?

    即。改变这个

    select new LikesandPictureDetails
    

    select new 
    

    如果是这样,那么您应该将目标对象中的值的类型更改为可为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2014-10-08
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多