在自动填充文本框中的linq中的OrderBy [英] OrderBy in linq in autocpmplete textbox

查看:74
本文介绍了在自动填充文本框中的linq中的OrderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

--scenario

i have the table as below

Id	UnitId	Name	     ParentId
10	345	xyz	          2
11	3149	Sandhurst	 2
12	3188	store1	          2
13	3181	store2	          2
14	3182	store3	          2
15	3183	store4	          2
16	3184	store5	          2
17	3185	store6	          2
18	3186	store7	          2
19	3133	store8	          2
20	3134	store9	          2
21	3135	store10	          2
22	32	xyz1	          2
23	37	xyz2	          2
24	37	xyz2	          2










--i have a autocomplete textbox. Now in the autocomplete textbox, user may enter UnitId or Name.

if UnitId is entered 3
result should be UnitId ascending

32    xyz1
37    xyz2
345   xyz
3149  Sandhurst

--if i enter Name result should be in order by Name ascending. If i enter 'x'
345   xyz
32    xyz1
37    xyz2

--But My linq doesnot fetch in order by. In database unitid is varchar.

kindly help me in writing the Linq that will order by .

So far my linq is as below.
------------------------------Linq---------------------------------------

public List<BusinessUnit> GetStoresNameId(string StoreIdName)
       {
           try
           {

               List<BusinessUnit> data = (from r in GetStoreHierarchy().Where(p => p.ParentId != "1" && p.ParentId != null).ToList()
                                          where r.UnitId.ToUpper().StartsWith(StoreIdName.ToUpper()) || r.Name.ToUpper().StartsWith(StoreIdName.ToUpper())
                                          select new BusinessUnit { UnitId = r.UnitId, Name = r.Name }).Distinct().Take(10).ToList();
               return data;

           }
           catch (Exception ex)
           {
               //_log.Error("GetStores", ex);
               throw ex;
           }

       }













-- I want to include orderby in my linq. 

-- If in the function <pre lang="vb">GetStoresNameId(string StoreIdName)



StoreIdName被传递,如果它是UnitId ,,,它将按单位排序...

如果是名称它将按名称排序..



- -----------这适用于unitid本身-----------------------


StoreIdName is passed and if it is UnitId,,,it will sort by unitid...
if it is Name it will sort by Name..

-------------This is working for unitid itself-----------------------

List<BusinessUnit> data = (from r in GetStoreHierarchy().Where(p => p.ParentId != "1" && p.ParentId != null).ToList()
                                         where r.UnitId.ToUpper().StartsWith(StoreIdName.ToUpper()) || r.Name.ToUpper().StartsWith(StoreIdName.ToUpper())
                                         select new BusinessUnit { UnitId = r.UnitId, Name = r.Name }).Distinct().OrderBy(x=>(Convert.ToInt32(x.UnitId))).Take(10).ToList();
              return data;





----------------但我想要为名字做这件事-----------------------



----------------but i want to do it for name too-----------------------

推荐答案

试试这个



try this

.OrderByDescending(x => x.Delivery.SubmissionDate);


这篇关于在自动填充文本框中的linq中的OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆