3
IQueryable<Employee> query = ((IEnumerable<Employee>)employeeList)
    .Select(x => x)
    .AsQueryable();

var strListEmployees = input.MustIncludeIdsInPage.Split(",").ToList();
//the list of employee is dynamic, it'd return 3, 4, 5 or more data

var entities = query
    .OrderBy(item => strListEmployees.IndexOf(item.Id.ToString()))
    .PageBy(input)
    .ToList();

example data

What I want is something like this in order: by employee name

D
F
A
B
C
E
G
H

Employee D, F, A on top (fix value in List) and show the rest with name sorting (order by).

GôTô
  • 7,974
  • 3
  • 32
  • 43
  • 1
    So You want to preselect some entities, get them from list in that order and to this selection add others. Others are sorted list of results without the list from start. – Tatranskymedved Feb 24 '17 at 13:10
  • yeah, something like that, but can i just use a linq for it? @Tatranskymedved – Robin Junior Feb 24 '17 at 13:13

3 Answers3

1

You can use OrderBy() and ThenBy()

List<Test> tests = new List<Test>()
           {
               new Test() {EmployeeID = "1", Name = "A"},
               new Test() {EmployeeID = "2", Name = "B"},
               new Test() {EmployeeID = "3", Name = "C"},
               new Test() {EmployeeID = "4", Name = "D"},
               new Test() {EmployeeID = "5", Name = "E"},
               new Test() {EmployeeID = "6", Name = "F"},
               new Test() {EmployeeID = "7", Name = "G"},
               new Test() {EmployeeID = "8", Name = "H"},
           };

var x = tests.OrderBy(name => name.Name != "D")
           .ThenBy(name => name.Name != "F")
           .ThenBy(name => name.Name != "A")
           .ThenBy(name => name.Name)
           .ToList();

Result is: First D,F,A and others names

enter image description here

Edit:

string[] filtr = new[] {"D", "F", "A"};
var fdata = tests.Where(d => filtr.Contains(d.Name)).OrderBy(z=>z.Name).ToList();
var odata = tests.Where(d => !filtr.Contains(d.Name)).OrderBy(z => z.Name).ToList();
fdata.AddRange(odata);
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
1

As M. Wiśnicki mentioned, this is easily solveable as You got only 3 elements. But to dynamically resolve this, I would stick to some function, where You would enter the List (or IEnumerable) of the objects and also the Names, based on which You want to filter them.

The code below is recursion, which will go through the array and select the 1st element (from array) and add the rest. Rest is calling the same function without the 1st name & without the element we have already added.

Something like:

public IEnumerable<Employee> GetOrderedPrefered(IEnumerable<Employee> aList, string[] aNames)
{
    if (aNames.Length == 0) return aList.OrderBy(a => a.Name).ToList();

    var lRes = new List<Employee>()
    {
        aList.FirstOrDefault(a => a.Name == aNames[0])
    };

    lRes.AddRange(
        GetOrderedPrefered(
            aList.Where(a => a.Name != aNames[0]),
            aNames.Where(a => a != aNames.First()
        ).ToArray()
    ));

    return lRes;
}

Usage:

var lRes = GetOrderedPrefered(persons, names);
foreach (var item in lRes)
    Console.WriteLine(item.Name);

> D
> F
> A
> B
> C
> E
> G
Community
  • 1
  • 1
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
0
var set = Enumerable.Range(0, 8)
                    .Select(i => new { 
                        Name = new string(new[] { (char)('A' + i) }) 
                    });

var before = string.Join(",", set.Select(i => i.Name)); //A,B,C,D,E,F,G,H


var priorities = "D,F".Split(',').Select((v, i) => new { Value = v, Index = i });

var query = from s in set
            join p in priorities on s.Name equals p.Value into m
            from x in m.DefaultIfEmpty(new { Value = s.Name, Index = int.MaxValue })
            orderby x.Index, s.Name
            select s.Name;

var result = string.Join(",", query); //D,F,A,B,C,E,G,H
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69