i have a problem to order a list by certain rule.
Actually i have Links Object List For Example List list ;
Links Class it’s build in this way :
private class Links
{
public Int32 IdHost { get; set; }
public String Url { get; set; }
}
Now using this Code i order the list by IdHost.
var listSortedById = source
.OrderBy(n => n.IdHost).ToList();
The problem now starting cause i wish that idHost == 2 should be put every 5 element in the list instead one each other.
Update Example
For Example : 2 1 1 1 3 3 3 4 4 4 1 1 1 2 3 3 3 4 4 4 1 1 1 3 3 3 2 4 4 4 i mean put idHost == 2 every 5 elements.
For what concern the element which comes first i would like that it shold be the idHost == 2.
Is there a way to do this with Linq?
Don’t make it so hard on yourself. If you think too hard you’ll reach for really fancy complicated solutions that use LINQ in non-intuitive ways. Instead, KISS.
First, sort the items that don’t have
IdHost == 2and put those items into a list:Then, collect the items that have
IdHost == 2:Finally, insert these items every 5th position into your list: