score:9
instead of switching between four separate queries, build single query according to your conditions:
iqueryable<score> query = db.arcadescores.where(c => c.gameid == gameid);
switch(type)
{
case highscoretype.scorerank:
query = query.where(c => c.scorerank > 0).orderby(c => c.scorerank);
break;
case highscoretype.userrank:
query = query.where(c => c.userrank > 0).orderby(c => c.userrank);
break;
}
if (skip.hasvalue && take.hasvalue)
query = query.skip(skip.value).take(take.value);
return query.tolist();
score:0
probably, extract this to a method so that you would only call orderby and skip when they want the paginated one.
db.arcadescores.where(c => c.gameid == gameid && c.scorerank > 0)
.orderby(c => c.scorerank).
db.arcadescores.where(c => c.gameid == gameid && c.userrank > 0)
.orderby(c => c.userrank)
into
private ienumerable<arcadescore> getarcadeoverallscore(int gameid)
{
return db.arcadescores.where(c => c.gameid == gameid && c.scorerank > 0)
.orderby(c => c.scorerank)
}
private ienumerable<arcadescore> getarcadeuserscore(int gameid)
{
return db.arcadescores.where(c => c.gameid == gameid && c.userrank > 0)
.orderby(c => c.userrank)
}
score:1
given that the ienumerable
is not executed until you call tolist
you can simply move those outside the switch statement and then call tolist
score:1
simply use the coalesce operator to provide default values:
.skip(skip ?? 0)
.take(take ?? int.maxvalue)
Source: stackoverflow.com
Related Query
- Avoiding repeating code with Linq query + optional params
- LINQ Filter query with multiple optional parameters
- Linq sub query when using a repository pattern with EF code first
- how to write a Linq query with a EF code first Many to Many relationship
- Performing a LINQ query with optional search clauses
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- How to create a database query with LINQ to erase rows by having a list of ID's and by avoiding loops
- Linq query with nullable sum
- Problem with linq query
- Nested "from" LINQ query expressed with extension methods
- How to use index/position with Where in LINQ query language?
- Linq query with Array in where clause?
- Casting to a derived type in a LINQ to Entities query with Table Per Hierarchy inheritance
- How do I most elegantly express left join with aggregate SQL as LINQ query
- How to query an XDocument with LINQ when elements have a colon in their name?
- LINQ query with Distinct and Union
- Selecting multiple columns with linq query and lambda expression
- GroupBy with linq method syntax (not query syntax)
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Simplify process with linq query
- LINQ To SQL: Delete entity (by ID) with one query
- How/Can I use linq to xml to query huge xml files with reasonable memory consumption?
- query a sub-collection of a collection with linq
- Linq query with multiple Contains/Any for RavenDB
- LINQ select query with Anonymous type and user Defined type
- LINQ to XML optional element query
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Entity Framework - Linq query with order by and group by
- Linq Query with SUM and ORDER BY
- Linq Query with a Where clause in an Include statement
More Query from same tag
- Finding the nth largest in an int array
- Join two IQueryable
- How use linq multi list criteria?
- Get value and index first low value in array
- How can I have different LINQ to XML queries based on two different condition?
- linq left outer join on lists
- Return the result of joining two tables in Silverlight 4.0
- First match in a collection
- Unable to cast string type to number type in Linq expression
- Getting error in linq to entity
- How to use LINQ select something contain string[]
- Group Multiple Properties with Logical OR
- Entity Framework Where Clause for Calculated property
- How to improve this LINQ query
- How would I use LINQ2XML in this scenario?
- "Argument data type ntext is invalid for argument 1 of upper function" when trying to add a third item to a filter
- How to convert an IQueryable<T> to a List<T>?
- Dynamic Linq Search using NinjaNye.SearchExtensions
- How to get value from nested KeyValuePair from IMongoCollection
- fetching Id for matching row - any performance difference between two approaches?
- CopyToDataTable in LINQ
- How do I convert a string array to a byte array using Array ConvertAll?
- System.NotSupportedException when creating a reversed list
- LINQ XML select inside select
- Easiest way to implement .Contains() on a ICollection<Tuple<T1,T2>>
- Get specific value from GroupBy Key in foreach
- LINQ .Select() Accessing parent properties?
- Exceptionally slow database response time on Linq query
- how to use group by in linq
- Compare lists and get values with Linq