Is there anyway this code can be refactored? The only difference is the order by part.
Idealy I'd like to use a delegate/lambda expression so the code is reusable but I don't know how to conditionally add and remove the query operators OrderBy and OrderByDescending
var linq = new NorthwindDataContext();
var query1 = linq.Customers
.Where(c => c.ContactName.StartsWith("a"))
.SelectMany(cus=>cus.Orders)
.OrderBy(ord => ord.OrderDate)
.Select(ord => ord.CustomerID);
var query2 = linq.Customers
.Where(c => c.ContactName.StartsWith("a"))
.SelectMany(cus => cus.Orders)
.OrderByDescending(ord => ord.OrderDate)
.Select(ord => ord.CustomerID);
You can create your own reusable extension method which will do this:
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>
(this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
bool ascending)
{
return ascending ? source.OrderBy(keySelector)
: source.OrderByDescending(keySelector);
}
and similarly for ThenBy
:
public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>
(this IOrderedQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
bool ascending)
{
return ascending ? source.ThenBy(keySelector)
: source.ThenByDescending(keySelector);
}
You can split your query up into bits, and use control flow logic. LINQ to SQL will magically construct the correct query as if you had typed it all one line! The reason this works is that the query is not sent to the database until you request the data, but instead is stored as an expression.
var linq = new NorthwindDataContext();
var query = linq.Customers
.Where(c => c.ContactName.StartsWith("a"))
.SelectMany(cus=>cus.Orders);
IOrderedQueryable<Order> query2;
if (useAscending) {
query2 = query.OrderBy(ord => ord.OrderDate);
} else {
query2 = query.OrderByDescending(ord => ord.OrderDate);
}
var query3 = query2.Select(ord => ord.CustomerID);
return from T in bk.anbarsabts
where T.kalaname == str
orderby T.date descending
select new { T.date, T.kalaname, T.model, T.tedad };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With