--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 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-----------------------