I have the following switch statement in my code. However when I declare the annonymous type "var query3" i get an error Implicitly-typed local variables must be initialized. what am I doing wrong?
//error is here
var query3;
switch ((string)ViewState["SortColumn"])
{
case "DueDate":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.DueDate ascending
select c);
break;
case "ARNumber":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.ActionNumber ascending
select c);
break;
case "ActionRequestType":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.ActionRequestType ascending
select c);
break;
var keyword doesn`t mean that you are declaring a "variant" variable of whatever type. The compiler needs to know what type you`re declaring and it is getting it from the variable intialization, which you do not have.
so do
var query3 = new WhateverTypeYouExpecting();
Alexei Fimine
_____________
"And though I have the gift of prophecy, and understand all mysteries, and all knowledge; and though I have all faith, so that I could remove mountains, and have not charity, I am nothing"
Error 26 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<ActionRequestObj>' to 'System.Collections.Generic.List<ActionRequestObj>'. An explicit conversion exists (are you missing a cast?)
AceHigh26
Member
13 Points
44 Posts
annonymous variable and linq query result
Nov 05, 2012 05:09 PM|LINK
I have the following switch statement in my code. However when I declare the annonymous type "var query3" i get an error Implicitly-typed local variables must be initialized. what am I doing wrong?
//error is here
var query3;
switch ((string)ViewState["SortColumn"])
{
case "DueDate":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.DueDate ascending
select c);
break;
case "ARNumber":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.ActionNumber ascending
select c);
break;
case "ActionRequestType":
query3 = (from c in myActionRequestObjArr.AsEnumerable()
orderby c.ActionRequestType ascending
select c);
break;
etc.....
Datagrid.Datasource = query3.ToList();
RameshRajend...
Star
7983 Points
2099 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 05:19 PM|LINK
Hai
just change this line
var query3 = new Address();//assign class(return claas) name
my Address means the Address was my table name....But u asiighn u r table name or u r return class name
Thank u
fimine
Contributor
3008 Points
549 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 05:24 PM|LINK
var query3;
var keyword doesn`t mean that you are declaring a "variant" variable of whatever type. The compiler needs to know what type you`re declaring and it is getting it from the variable intialization, which you do not have.
so do
var query3 = new WhateverTypeYouExpecting();
_____________
"And though I have the gift of prophecy, and understand all mysteries, and all knowledge; and though I have all faith, so that I could remove mountains, and have not charity, I am nothing"
AceHigh26
Member
13 Points
44 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 05:34 PM|LINK
I tried your suggestion and got:
Error 26 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<ActionRequestObj>' to 'System.Collections.Generic.List<ActionRequestObj>'. An explicit conversion exists (are you missing a cast?)
RameshRajend...
Star
7983 Points
2099 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 05:39 PM|LINK
Hai try it
dont use var .So change to var to List
for
List query3 =new ActionRequestObj();
and check or add this namespace
using System.Collections.Generic;
Thank u
AceHigh26
Member
13 Points
44 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 06:40 PM|LINK
It I the above " List var = new ActionRequestObj();
List is not valid and Generic keyword has been added to the namespace already.
RameshRajend...
Star
7983 Points
2099 Posts
Re: annonymous variable and linq query result
Nov 05, 2012 06:45 PM|LINK
Ok
try this
List<ActionRequestObj> query3=new ActionRequestObj();