我试图在数组索引的基础上对查询进行排序。请参阅以下代码 -
query = data.Users;
query = query.Where(u=>u.IsActive);
query = query.OrderBy(u=>u.LName);
query = query.OrderBy(u=>u.Phone[0]);
除了最后一行,一切正常。它显示以下错误 -
NotSupportedException was unhandled.
The expression [10007].Phone.get_Item(0) is not supported.
修改
我试过了 -
query = query.OrderBy(u=>u.Phone.First());
query = query.OrderBy(u=>u.Phone.FirstOrDefault());
手机是一个字符串数组。
答案 0 :(得分:0)
我认为您需要使用ThenBy而不是第二个orderby:
query = data.Users;
query = query.Where(u=>u.IsActive);
query = query.OrderBy(u=>u.LName);
query = query.ThenBy(u=>u.Phone[0]);
否则你只需重新排序整个数组,而不是按你想要的2个字段排序。