在linq中按字符串列排序会导致错误

时间:2014-04-26 17:23:24

标签: c# linq

我有这样的查询:

 List<PresentClass.userpresentation> q =
     (dbconnect.tblUsers.Where(
       i => i.permission == permission)
       .Select(arg => new PresentClass.userpresentation { 
       email = arg.email, pass = arg.password, 
       name = arg.name+" "+arg.family })).ToList();

添加 orderby 后:

 List<PresentClass.userpresentation> q =
     (dbconnect.tblUsers.Where(
     i => i.permission == permission)
     .Select(arg => new PresentClass.userpresentation { 
     email = arg.email, pass = arg.password, 
     name = arg.name+" "+arg.family })).OrderBy(i=>i.family).ToList();

我收到了这个错误:

  

该成员   'Novitiate.AdminPortal.PresentationClass.PresentClass + userpresentation.family'   没有受支持的SQL翻译。

我的课程:

 public class userpresentation
 {
    public string username { set; get; }
    public string email { set; get; }
    public string family { set; get; }
    public string name { set; get; }
    public  string pass{ set; get; }
 }

为什么?

1 个答案:

答案 0 :(得分:1)

看起来它正在尝试将OrderBy()转换为投影上的SQL语句。

如果您希望数据库执行排序,请尝试在Select()之前添加OrderBy();如果要在加载集合后进行排序,请尝试在ToList()之后添加。

var q = (dbconnect.tblUsers.Where(i => i.permission == permission)
                           .OrderBy(i=>i.family)
                           .Select(arg => new PresentClass.userpresentation { 
                               email = arg.email, 
                               pass = arg.password, 
                               name = arg.name+" "+arg.family 
                            })).ToList();