Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
C#
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);
Posted
Updated 22-Feb-15 0:38am
v2
Comments
Richard MacCutchan 22-Feb-15 7:46am    
Why are you trying to convert values to integers? They should already be stored in that way.
Maciej Los 29-Mar-15 7:00am    
Can you provide sample data?
Maciej Los 29-Mar-15 7:03am    
I think the main problem is in this line: var mainQuery = ... where idQuery.Contains(Convert.ToInt32(p.PollID)). You should compare values this way: idQuery.PollID == p.PollID

Please, read my comments to the question.

I'd suggest to change your queries into single one:
C#
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)
          };
 
Share this answer
 
Try int.Parse() instead of Convert.ToInt32()
C#
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 :)
 
Share this answer
 
Comments
saeed1364 22-Feb-15 6:52am    
The not correct answer
db.pollquestions.Where(x=>x.IsCurrent==true).SingleOrDefault();
Line 56: var mainQuery = from p in db.pollOptions where idQuery.Contains(Convert.ToInt32(p.PollID)) select new { p.OptionID, p.PollID, p.QuestionText, vp = int.Parse(p.Votes.ToString()) };
Line 57: var sumvotes = mainQuery.Sum(a => a.vp);
Suvendu Shekhar Giri 22-Feb-15 7:01am    
Try with link provided in solution 2.
saeed1364 22-Feb-15 7:05am    
Error 36 'int' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900