Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--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---------------------------------------
XML
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 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-----------------------
XML
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-----------------------
Posted
Updated 11-Mar-14 0:55am
v2

1 solution

try this

C#
.OrderByDescending(x => x.Delivery.SubmissionDate);
 
Share this answer
 
Comments
anurag19289 11-Mar-14 23:50pm    
how to include two columns in one order by. I m stuck here..

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;

--i have done for one column.
anurag19289 11-Mar-14 23:52pm    
user may enter either unitid or name. If UnitId is enterd order by unitId(Not by Name).
If Name is enterd then order by Name(not by UnitId)

--so in the below line i need to do order by UnitId and Name. how to do that.

select new BusinessUnit { UnitId = r.UnitId, Name = r.Name }).Distinct().OrderBy(x=>(Convert.ToInt32(x.UnitId))).Take(10).ToList();
AndrewCharlz 12-Mar-14 0:07am    
in sql we do this like this order by name asc , Unitid desc

in linq try this .Orderby(c => c.Name).ThenBy(n => n.unitID)
anurag19289 12-Mar-14 10:27am    
ok I tried the below query.

select new BusinessUnit { UnitId = r.UnitId, Name = r.Name }).Distinct().Take(10).OrderBy(c => Convert.ToInt32(c.UnitId)).ThenBy(c => c.Name.StartsWith(StoreIdName)).ToList();


it works for unitid. But if I start with name it doesnot work. As orderby is mentioned first for unitid followed by name.

For example.

unitid name
121 sz
122 sa
122 sc

if I type 12 in my autocomplete textbox. I get correct result
unitid name
121 sz
122 sa
123 sc

but If I type 's' in the textbox
result:
unitid name
121 sz
122 sa
123 sc

--but when I type 's' the result should be
unitid name
122 sa
123 sc
121 sz


--so is it possible to do it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900