怎么解决这个错误 [英] how sloved this error

查看:72
本文介绍了怎么解决这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam us ethis查询但看到此错误

LINQ to Entities无法识别方法'Int32 ToInt32(System.Object)'方法,并且此方法无法转换为商店表达式。

第57行:var sumvotes = mainQuery.Sum(a => a.vp);

iam us ethis query but see this error
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression.
Line 57: var sumvotes = mainQuery.Sum(a => a.vp);

var idQuery = from q in db.pollquestions where q.IsCurrent select q.PollID;
          var qpollid =db.pollquestions.Where(x=>x.IsCurrent==true).SingleOrDefault();
          var mainQuery = from p in db.pollOptions where idQuery.Contains(Convert.ToInt32(p.PollID)) select new { p.OptionID, p.PollID, p.QuestionText, vp = Convert.ToInt32(p.Votes) };
          var sumvotes = mainQuery.Sum(a => a.vp);

推荐答案

请阅读我对这个问题的评论。



我建议将您的查询更改为单一查询:

Please, read my comments to the question.

I'd suggest to change your queries into single one:
var qry = from pq in db.pollquestions.Where(x=>x.IsCurrent==true) 
          join po in db.pollOptions on pq.PollID equals po.PollID
          group po by po.PollID into grp
          select new
          {
              PollID = grp.Key,
              Question = grp.pq.QuestionText,
              SumVotes = grp.Sum(p=>p.Votes)
          };


尝试 int值。 Parse()而不是 Convert.ToInt32()

Try int.Parse() instead of Convert.ToInt32()
var idQuery = from q in db.pollquestions where q.IsCurrent select q.PollID;
            var qpollid =db.pollquestions.Where(x=>x.IsCurrent==true).SingleOrDefault();
            var mainQuery = from p in db.pollOptions where idQuery.Contains(int.Parse(p.PollID.ToString())) select new { p.OptionID, p.PollID, p.QuestionText, vp = int.Parse(p.Votes.ToString()) };
            var sumvotes = mainQuery.Sum(a => a.vp);





希望,它有帮助:)



Hope, it helps :)


这篇关于怎么解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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