score:10
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);
Source: stackoverflow.com
Related Query
- converting LINQ query result to int
- Using LINQ query result for data source for GridControl c#
- Converting foreach loop to LINQ query breaks code
- LINQ query result in int type variable
- Can I assign the result of a Linq query to the source variable of the same query?
- Converting SQL query result to Linq result with an new field that is not in the Model
- Linq query to get result of query as array of int
- Convert Linq Query Result to Dictionary
- Filling a DataSet or a DataTable from a LINQ query result set
- How to convert LINQ query result to List?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Returning a single property from a LINQ query result
- View result of LINQ query in watch/debugger
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- why is this linq query return a boolean and not the first result of the select?
- Using LINQ To Query Int Ids From An Array
- linq query where int ID belongs to List<int>
- adding index to linq query result
- format date in linq query result
- Result of LINQ Query in MVC SelectList as Value and Text - not working
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How do you transform a Linq query result to XML?
- Will the result of a LINQ query always be guaranteed to be in the correct order?
- Return one result from LINQ Query
- Pass int array in where clause of LINQ Query
- LINQ Query Returning Multiple Copies Of First Result
- Linq Query Returns Incorrect Result Set
- How to cast a Linq Dynamic Query result as a custom class?
- LINQ Source Code Available
- Converting a LINQ query into a Dictionary<string, string[]>
More Query from same tag
- Populate a tree from Hierarchical data using 1 LINQ statement
- Array.Reverse() versus Enumerable.Reverse<TSource>()
- C# Method to utilise linq expression
- Projecting an IEnumerable inside an Projected IQueryable making N Requests to the Database
- Return objects which contain Lists, based on the those lists matching
- Benefits between .ToList().First() verse .Single for IQueryable in c#
- query conversion is giving can not convert error in linq to sql
- SqlParameter is already contained by another SqlParameterCollection, but I don't see how
- Linq to Entity Framework returned value results in error if not debugged
- How can I calculate an integral?
- Trying to add a ENUM to a IQueryable
- Loop hierarchy to root
- Why does the Entity Framework generate nested SQL queries?
- How do I remove a row in one table based on another table?
- How to get multiGroup IEnumerable dynamically
- How to write a statement that generates a LIKE T-SQL statement in System.Linq.Dynamic
- Search in nested list
- How to convert SQL into Linq
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- How to append more rows to a Linq list after the condition is met
- Getting Nth value with Linq
- What's the difference between these two statements?
- Linq Aggregation Statement To Process Values Null and Not Null Values
- Nhibernate Linq is null on left join
- query xmlnode using linq
- HashSet<T> quickly checking if identity exists by T.identity
- dynamic linq query with or and like
- C# linq sort - quick way of instantiating IComparer
- How to bind XAttribute Value by Foreach
- How to properly check IEnumerable for existing results