score:10

Accepted answer

that is because res.firstordefault() returns a routelinqs type object, because you are using select p , you may select a field using select p.fieldname, where fieldname is the property you require for conversion, which is probably routeid

you may want to query the particular field in your linq query.

var res = (from p in aspdb.routelinqs
                       orderby p.routeid descending
                       select p.routeid).take(1); //p.routeid or any field you want. 

or with your current query, you may do:

 route.id =convert.toint32(res.firstordefault().routeid);

score:0

first make linq :

iqueryable<int> query = (from p in aspdb.routelinqs
                           orderby p.routeid descending
                           select p.id);

than run:

 int result = query.firstordefault();

in 1 command:

int result = (from p in aspdb.routelinqs
                           orderby p.routeid descending
                           select p.id).firstordefault();

score:1

not sure how your is but routelinqs is defined, but perhaps you are looking to select a specific object from the class routelinqs.

var res = (from p in aspdb.routelinqs
                   orderby p.routeid descending
                   select p.someproperty).take(1);

in this case p is a single item of routelinqs and p.someproperty should be your int value.

score:2

this code should work:

route.id = convert.toint32((res.firstordefault()).fieldname); 

score:3

looks like you're selecting the max route id, so why not just do:

route.id = aspdb.routelinqs.max(x => x.routeid);

Related Query

More Query from same tag