Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have view that show all of my products,
i want to able show specific prodouct in top of list
how can i do this?

What I have tried:

show product using ef code first
Posted
Updated 30-Jun-19 5:38am
Comments
[no name] 28-Jun-19 13:17pm    
Put the "specific product" at the top of the list before "showing" it. Delete / insert. Sort. Concatenate. Take your pick.
AminMhmdi 28-Jun-19 13:30pm    
no, currently i show product with it sorted id =>> 1,2,3,4,5...
i want show product 5 in second place and product with id 4 in first place
MadMyche 28-Jun-19 14:11pm    
Add a "Sort Order" column to the data-set

1 solution

Assuming that you have a
List<Product>
. You want to order that products based on another list. So, you can use something like this:

C#
List<int> sortOrder = new List<int>(){4, 5};
	var orderedProducts = context.products
		.ToList()
		.OrderBy(p=>sortOrder.IndexOf(p.ProductID)==-1 ? 2 : sortOrder.IndexOf(p.ProductID))
		.ToList();


This should return a list of products sorted this way:
4 
5 
1 
2 
3 
6 
7 
8 
9 

Good luck!
 
Share this answer
 
Comments
AminMhmdi 3-Jul-19 23:18pm    
tnx a lot , can you explain OrderBy(p=>sortOrder.IndexOf(p.ProductID)==-1 ? 2 : sortOrder.IndexOf(p.ProductID))???
Maciej Los 4-Jul-19 3:06am    
The answer is very simple: products are ordered by the index of product on sortOrder list. If product id couldn't be found (-1), then set order to 2. For further details, please read this: ?: operator - C# reference | Microsoft Docs[^]

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