Reputation: 779
Hi just needed suggestion regarding List /LINQ etc
public static List<Employee> Employees()
{
return Adapters.WebServiceAdapter.getInstance.SELECT_EMPLOYEES().ToList();
}
This is returning USERID , USERNAME in ASC order of USERID as below
USERID USERNAME
1 FirstUser
2 SecondUser
3 ThirdUser
4 FourthUser
5 FifthUser
I have requirement of rearranging the output based on the currently logged Employee.
For Example i have information that USERID 4TH IS LOGGED in my website then this function should return me
USERID USERNAME
4 FourthUser
1 FirstUser
2 SECONDUSER
3 THIRDUSER
5 FIFTHUSER
Just to give reason why i am doing this - I have a drop-downs various places in my website and i want to ensure that if this function is called to load my dropdown lists . By default the currently logged user should be selected by default.
I appreciate if someone could provide valuable inputs..
Upvotes: 0
Views: 60
Reputation: 4532
You can also try this trick: Insert your logged user in position 0 of the list, then call distinct.
MyList.Insert(0,MyList.Single(c => c.UserID == IDofLoggedUser));
var lst = MyList.Distinct();
Result:
the original list ordered by ID like you have
MyList = 1,2,3,4,5,6;
then insert for example logged user has ID of 4
MyList = 4,1,2,3,4,5,6;
then call distinct,it will find and maintain the first 4,and remove second 4, so final result:
MyList = 4,1,2,3,5,6;
Upvotes: 0
Reputation: 67115
Just combine two lists using Union
Link's Code:
var numbers = new List<Int32>{1,2,3,4,5};
var loggedInUsers = numbers.Where(x=>x == 4);
var restOfUsers = numbers.Where(x=>x != 4);
var joinedLists = loggedInUsers.Union(restOfUsers).ToList();
Upvotes: 1