开发者

LINQ to Entites/IQueryable: Sorting on multiple fields

I have the following that I'd like to sort:

IQueryable<Map&开发者_JAVA百科gt; list;
list = from item in ctx.MAP
         .Include("C")
         .Include("L")
         .Include("L.DP")
       select item;
return list.OrderBy(m=>(m.L.DP.Name + m.L.Code));

This works, but it sorts alphabetically - so 12 comes before 9. (Assume Code is a numeric field)

What is the best way to sort this so Code is sorted numerically?


You probably want to use the ThenBy extension method to be able to sort by multiple fields ;) In your case that would be

 return list.OrderBy(m=>m.L.DP.Name).ThenBy(m => m.L.Code);


 var results = db.Blogs.AsEnumerable()
                    .Select(sr => new
                    {
                        Searchresult = sr,
                        Words = Regex.Split(sr.Name, @"[^\S\r\n {1,}").Union(Regex.Split(sr.Name2, @"[^\S\r\n]{1,}"))
                    })
                    .OrderByDescending(x => x.Words.Count(w => {
                        foreach (var item in searchTerms)
                        {
                            if(w.ToLower().Contains(item))
                            {
                                return true;
                            }
                        }
                           return false;
                    }))
                    .Select(x => x.Searchresult);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜