在linq c#中从db中选择数据后如何按id排序输出
How to get sorted output by id after selecting data from db in linq c#
我有一个 integers ids
的列表,来自 table 我需要 select 仅那些 ID 在列表中的人的详细信息,I have already done that
但我需要那个data sorted same way as they were in the list Explaination below:
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();
现在,当我 运行 这个查询时,我从数据库中得到了详细信息列表,但它在 list (SelectedUser).
中的 sorted form but i need to sort them by the form which they were
中
它不太漂亮,但您可以使用您的第一个列表,然后使用第二个列表中的 select FirstOrDefault
。
如果在 objValidateUserList
列表中找不到第一个 SelectedUser
,请确保最后一个 Where
不包含空值。
var sortedList = SelectedUser.Select(
sortedListUser => objValidateUserList.FirstOrDefault(nonSortedListUser => nonSortedListUser.value == sortedListUser))
.Where(x => x != null);
希望对您有所帮助!
我有一个 integers ids
的列表,来自 table 我需要 select 仅那些 ID 在列表中的人的详细信息,I have already done that
但我需要那个data sorted same way as they were in the list Explaination below:
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();
现在,当我 运行 这个查询时,我从数据库中得到了详细信息列表,但它在 list (SelectedUser).
sorted form but i need to sort them by the form which they were
中
它不太漂亮,但您可以使用您的第一个列表,然后使用第二个列表中的 select FirstOrDefault
。
如果在 objValidateUserList
列表中找不到第一个 SelectedUser
,请确保最后一个 Where
不包含空值。
var sortedList = SelectedUser.Select(
sortedListUser => objValidateUserList.FirstOrDefault(nonSortedListUser => nonSortedListUser.value == sortedListUser))
.Where(x => x != null);
希望对您有所帮助!