My database table has the following:
ID PollID Count
1 6 0
2 6 1
Obviously doing a: SELECT SUM([COUNT]) WHERE POLLID = 6 would return 1
However, this LINQ code is returning 2.
long totalVoteCount = (from pa2 in df.PollAnswers
where pa2.Poll.ID == pollID
select pa.Count).Sum();
While writing this question, I noticed that the above LINQ code is returning incorrect data for other queries. What exactly am I doing wrong here? I want to SUM the COUNT column for a prticular POLLID.
You’re projecting
pa.Countfrom your query instead ofpa2.Count.Therefore, if you have a
pavariable in scope that refers to an object whoseCountproperty is1, and there are two items indf.PollAnswers, your query will indeed always return2.