Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How can I sort correctly?
Message
 
 
To
07/07/2017 16:42:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
C#
Category:
LINQ
Miscellaneous
Thread ID:
01652557
Message ID:
01652560
Views:
46
>>Hi everybody,
>>
>>I wrote this code, but it has one flaw and I don't see a way to solve it:
>>
>>
>>var items = _itemAdapter.GetAllList().Where(i => modifierGroup.ModGrpLnk.Any(m => m.ItemId == i.ItemId)).ToList();
>>
>>So, the modifierGroup comes with the list of ModGrpLnk entries attached to it. This ModGrpLnk class has SortOrder column. I'd like to return items belonging to the modifier group sorted according to that SortOrder.
>>
>>Unfortunately, I don't see a simple way of modifying the above LINQ query to return items in the right order.
>>
>>Do you see what can I do here?
>>
>>Thanks in advance.
>>
>>UPDATE. I think I need a JOIN query here and not the way I wrote it. I'll try to find some JOIN Linq query examples to write what I need.
>>UPDATE2. Apparently I had examples of that JOIN query already, so I just used it, testing now.
>
>
>-Sorting is very simple in Linq.
>-If you have your primary/foreign keys and relations set in your database, then you don't need join. Joins are seldom needed with Linq,
>
>However, your question is not understood. You are saying it comes with ModGrpLnk entries, while your Linq says it is not the case (if it is really the case then that .Any is nonsense, would mean checking a property is equal to itself).
>
>It would look like:
>
>
>.OrderBy( i => i.PropertyName)
>
The query as it was written was getting rows from the Items table matching the items in the ModGrpLnk. I think it was not even efficient and most likely it was translated into
select some columns from items i where exists (select * from ModGrpLnk m where m.ItemId = i.ItemId and m.GroupId = @GroupId)
I didn't actually looked into Profiler, but that's the idea behind my original query, I think.

I needed columns from the Items table but sorted in accordance to the ModGrpLnk. So, obviously I could not add the OrderBy clause to my original query as it was coming from the Items.

That's why I re-wrote it into JOIN query. I don't see another way to get columns from the Items but sort based on the SortOder from the ModGrpLnk. I think JOIN in this particular case is exactly what I need.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform