2016-11-16 67 views
0

我有integers ids列表,从表中我只需要为那些ID在列表中,I have already done that选择的细节,但我需要一个data sorted same way as they were in the list Explaination below:在LINQ C#从数据库中选择数据后如何获得通过编号的顺序排列输出

现在
List<int> SelectedUser = new List<int> { 26,1,22,27 }; 
List<ValidateDropDownBind> objValidateUserList = (from p in context.tblUser 
                    join q in context.tblTaskReportingAuthority on p.UserID equals q.UserID into gj 
                    from r in gj.DefaultIfEmpty() 
                    where p.IsActive == true && p.CompanyID == CurrentAdminSession.CompanyID && SelectedUser.Contains(p.UserID) 
                    //orderby r.TaskReportingAuthorityID 
                    select new ValidateDropDownBind 
                    { 
                     value = p.UserID, 
                     name = p.FirstName + " " + p.LastName 
                    }).GroupBy(x => x.value, (key, group) => group.FirstOrDefault()).ToList(); 

,因为我运行此查询我得到的从数据库的详细信息列表中,但它是在sorted form but i need to sort them by the form which they werelist (SelectedUser).

+0

我已经删除以前使用的顺序,但仍然面临同样的问题出来是在排序的形式。 –

回答

1

这是不是太漂亮,但你可以使用你的第一个列表,然后从第二选择列表与FirstOrDefault

最后Where是要确保你不包含空值,如果第一SelectedUser没有在objValidateUserList列表中找到。

var sortedList = SelectedUser.Select(
     sortedListUser => objValidateUserList.FirstOrDefault(nonSortedListUser => nonSortedListUser.value == sortedListUser)) 
     .Where(x => x != null); 

希望它有帮助!

+0

非常感谢它的工作:) –

相关问题