简体   繁体   English

LINQ OrderBy()。ThenBy()无法正常工作

[英]LINQ OrderBy().ThenBy() not working

I am converting a project of mine from using an SQL Server based Entity Framework model, to using a local SQLite database. 我正在将我的项目从使用基于SQL Server的实体框架模型转换为使用本地SQLite数据库。 So far everything is going well, but for some reason I am not able to sort queries on multiple columns. 到目前为止,一切进展顺利,但是由于某种原因,我无法对多列进行查询排序。 For example: 例如:

using (var db = new SQLiteConnection("test3.db"))
{
    var query = from a in db.Table<Account>()
        where a.Inactive == false
        orderby a.AccountName, a.AccountNickname
        select a;
    foreach (var account in query)
    {
        accounts.Add(account);                    
    }
}
AccountsGrid.ItemsSource = accounts;

Gives me the error Cannot resolve symbol 'ThenBy' , but if I change the ordering to: 给我错误Cannot resolve symbol 'ThenBy' ,但是如果我将顺序更改为:

orderby a.AccountName

then the query works fine. 然后查询工作正常。 I have also tried using .OrderBy(a => a.AccountName).ThenBy(a => a.AccountNickname) but I get the same error. 我也尝试过使用.OrderBy(a => a.AccountName).ThenBy(a => a.AccountNickname)但是我遇到了同样的错误。 I am already including using System.Linq; 我已经包括using System.Linq; but ReSharper is telling me that the using directive is not required, so that seems fishy also. 但是ReSharper告诉我,using指令不是必需的,所以这似乎也很麻烦。 Does anyone have any ideas what I could be missing? 有谁知道我可能会缺少什么?

Looking at the source , it seems the author doesn't understand LINQ properly: 从源头上看,作者似乎不太了解LINQ:

  • They haven't provided a ThenBy method, but instead they're collecting multiple orderings with multiple OrderBy calls 他们没有提供ThenBy方法,而是通过多个OrderBy调用来收集多个订单
  • They haven't exposed the table as an IQueryable<T> implementation (which is why you don't need using System.Linq ) - while this is a valid approach, it's a pretty unusual one 他们没有将表公开为IQueryable<T>实现(这就是为什么您不需要using System.Linq )-尽管这是一种有效的方法,但这是一种非常不寻常的方法

I would personally be pretty nervous about using this - the fact that it's "organized" as three huge source files is slightly alarming too. 我个人会对使用它感到非常紧张-它被“组织”为三个巨大的源文件这一事实也有点令人震惊。 You may want to try using LinqConnect instead - although I haven't used that, either. 您可能想改用LinqConnect-尽管我也没有用过。

If you do want to stick with the implementation you're using, I suspect that this would work - but it wouldn't work with other LINQ providers: 如果您确实想坚持使用所使用的实现,我怀疑这会起作用-但不适用于其他LINQ提供程序:

var query = from a in db.Table<Account>()
    where a.Inactive == false
    orderby a.AccountName
    orderby a.AccountNickname // Warning! Not portable!
    select a;

Normally having two orderby calls like this would be a really, really bad idea - but it seems that that's what the LINQ provider wants in this case. 通常,这样进行两次orderby调用是一个非常非常糟糕的主意 -但在这种情况下,这似乎是LINQ提供程序想要的。

    In Linq Query

var query = (from a in db.Table<Account>()
            where a.Inactive == false
            orderby a.AccountName ascending, a.AccountNickname descending
            select m);

      In Lambda Expression

var query = db.Table<Account>().where(a => a.Inactive == false).OrderBy(a => a.AccountName).ThenByDescending(a =>a.AccountNickname);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM