var rrrr = (from item in dbContext.Blogs
orderby item.LastUpdated descending
select new
{
item,
Rank = item.Title.Contains("title") ? 4 :
item.Author.Contains("author") ? 3 :
item.Tag.Contains("tagname") ? 2 :
0
}).OrderByDescending(i=>i.Rank);
Is working for me.. i am not sure about the perfomance of this querry in a working environment (extract from thousands of records ) and sort in memory .. Is it a good way?
mMATHAI
Member
2 Points
38 Posts
Order by in dynamic Linq expression
Apr 04, 2012 06:26 AM|LINK
How can i use orderby to a colum Rank ( which i s created dynamicaaly based on some conditions) instaed of LastUpdated
var rrrr = (from item in dbContext.Blogs orderby item.LastUpdated descending select new { item, Rank = item.Title.Contains("title") ? 4 : item.Author.Contains("author") ? 3 : item.Tag.Contains("tagname") ? 2 : 0 });Is it possible to use OrderBy on Rank (cynamically created column ) ? If yes how can can i do that else what are the other ways to do achieve this
ignatandrei
All-Star
135174 Points
21682 Posts
Moderator
MVP
Re: Order by in dynamic Linq expression
Apr 04, 2012 06:44 AM|LINK
make a .ToList and then make an OrderBy in memory
or
make a calculated column in database( preferred)
mMATHAI
Member
2 Points
38 Posts
Re: Order by in dynamic Linq expression
Apr 04, 2012 07:03 AM|LINK
var rrrr = (from item in dbContext.Blogs orderby item.LastUpdated descending select new { item, Rank = item.Title.Contains("title") ? 4 : item.Author.Contains("author") ? 3 : item.Tag.Contains("tagname") ? 2 : 0 }).OrderByDescending(i=>i.Rank);Is working for me.. i am not sure about the perfomance of this querry in a working environment (extract from thousands of records ) and sort in memory .. Is it a good way?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Order by in dynamic Linq expression
Apr 06, 2012 01:30 AM|LINK
I think so——because your Rank doesn't belong to your table and it's only a type of an anoymous property of the anoymous class……。
Reguards!
mMATHAI
Member
2 Points
38 Posts
Re: Order by in dynamic Linq expression
May 07, 2012 04:36 AM|LINK
I am not sure about the perfomance, but it works fine for me on a table with thousands of rows..