Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have query

SELECT votingtext, COUNT(Votingtext) AS count FROM votes group by Votingtext

which gives me count of votes now i wnated to convert taht query to Linq so i write follwoing piece of code

XML
public List<Vote> GetChartData()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            //SELECT votingtext, COUNT(Votingtext) AS count FROM votes group by Votingtext

            List<Vote> votes = GetAllVote();


            var query =
                 from c in votes
                 select new { c.VotingText, VoteCount = c.VotingText.Count() };

          //  return query.ToList(Vote); This line give me an error




        }


XML
public List<Vote> GetAllVote()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var query = from c in db.Votes  select c;
            //return query.Take(77).ToList();
            return query.ToList();

        }


i need to correct error mentioned in line
Posted

Can you post the exact error you are getting?
 
Share this answer
 
MSIL
C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2
\VotingPanel2.Web\Service3.svc.cs   37  33  VotingPanel2.Web</pre>

  C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs   37  33  VotingPanel2.Web</pre>
 
Share this answer
 
<pre lang="msil">Error   2   'VotingPanel2.Web.Vote' is a 'type' but is used like a 'variable'   C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs   37  33  VotingPanel2.Web




MSIL
Error   1   No overload for method 'ToList' takes '1' arguments C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs   37  20  VotingPanel2.Web
 
Share this answer
 
Comments
MalikRizwan 23-Sep-10 4:13am    
Hi Irum
use this
query.ToList<vote>();

please vote if it helps
MalikRizwan 23-Sep-10 4:14am    
please ignore my last message
use this
query.ToList< Vote >();

with no spaces in between < >
return query.ToList<Vote>();


vote if it helps
 
Share this answer
 
i m getting an error
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<anonymoustype#1>' to 'System.Collections.Generic.IEnumerable<votingpanel2.web.vote>' C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 37 20 VotingPanel2.Web

Error 2 'System.Collections.Generic.IEnumerable<anonymoustype#1>' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList<tsource>(System.Collections.Generic.IEnumerable<tsource>)' has some invalid arguments C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 37 20 VotingPanel2.Web


when i changed
     public List<vote> GetChartData()<br />
        {<br />
            DataClasses1DataContext db = new DataClasses1DataContext();<br />
            //SELECT votingtext, COUNT(Votingtext) AS count FROM votes group by Votingtext<br />
            List<vote> votes = GetAllVote();<br />
<br />
            var query =<br />
                 from c in votes<br />
                 select new { c.VotingText, VoteCount = c.VotingText.Count() };<br />
            return query.ToList<vote>();<br />
<br />
<br />
        }<br />
</vote></vote></vote>
 
Share this answer
 
Comments
MalikRizwan 23-Sep-10 4:22am    
Irum check my last message

that was about return query.ToList<Vote>();
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<anonymoustype#1>' to 'System.Collections.Generic.IEnumerable<votingpanel2.web.vote>' C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 37 20 VotingPanel2.Web


Error 2 'System.Collections.Generic.IEnumerable<anonymoustype#1>' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList<tsource>(System.Collections.Generic.IEnumerable<tsource>)' has some invalid arguments C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 37 20 VotingPanel2.Web


whne i chnaged it as suggested by u
 
Share this answer
 
Comments
MalikRizwan 23-Sep-10 4:39am    
it looks like when you select from query you are creating anonymous class.. since you cant return anonymous type so you need to do it something like this


select new Vote{ .... };
MalikRizwan 23-Sep-10 5:18am    
does this help?
I think u r saying this
C#
var query =
                from c in votes
                select new Vote  {c.VotingText, VoteCount = c.VotingText.Count() };



Error 1 Invalid initializer member declarator C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 35 36 VotingPanel2.Web

Error 2 'VotingPanel2.Web.Vote' does not contain a definition for 'VoteCount' C:\Users\Iram\Documents\Visual Studio 2008\Projects\VotingPanel2\VotingPanel2.Web\Service3.svc.cs 35 50 VotingPanel2.Web
 
Share this answer
 
can nay one else particapte on this problem


XML
public List<Vote> GetChartData()
     {



         DataClasses1DataContext db = new DataClasses1DataContext();
         //SELECT votingtext, COUNT(Votingtext) AS count FROM votes group by Votingtext

        
         var query3 = from c  in db.Votes
                      group c by c.VotingText into result
                      select new
                      {
                          Name = result.Key,
                          Count = result.Count()
                      };

         return query3.ToList<Vote>;




     }


error on last line return query3.ToList<Vote>;
 
Share this answer
 

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