2015-11-12 30 views
0

问题:“方法'Skip'只支持LINQ to Entities中的排序输入。方法'OrderBy'必须在方法'Skip'之前调用。如何按当前订单排列<object>列表,以便PagedList可以接受?

list2 = list1.Where(a).OrderBy(x => x.Something).Union(list1.Where(x).OrderBy(x => x.SomethingDifferent)); 
return list2.ToPagedList(...); 

我假设发生这种情况是因为它需要在'联盟'后添加一个新的'OrderBy'。有没有办法把它变成一个OrderedList,同时保持联盟目前的订单?

有没有办法添加其他排序依据,它实际上并不更改顺序,如:

list2 = list1.Where(a).OrderBy(x => x.Something).Union(list1.Where(x).OrderBy(x => x.SomethingDifferent)).OrderBy(x => x.NothingThatWillAffectTheOrder); 

编辑

为了使这更清楚,我将解释用一个实际的例子。

假设用户在电影数据库中进行搜索,并且想在显示包含搜索字符串的演员姓名前显示包含搜索字符串的标题。

list = movies.Where(x => x.Title.Contain...).Union(movies.Where(x => x.Actors.Contain..) 

PagedList不会接受这个,因为它没有排序,但是排序这个列表将会失败联盟的目的。是否有一个“解决方法”,使其成为有序列表,同时保持当前的顺序?

+0

我删除了我以前的答案,因为是可行的:但是这取决于你实际上是用它做什么,也许你可以只是像压平我没有完全理解你的问题。从https://github.com/TroyGoode/PagedList使用PagedList时,我没有遇到有关“跳过”方法的问题。那是你正在使用的那个? –

+0

是的,那是我正在使用的那个。问题是因为两个OrderBys发生在Union之前,所以PagedList不会接受它。我会编辑我的问题以提供更好的示例。 – Tom

+0

我看不出为什么会出现该错误,但我添加了一个全新功能实现的新答案。 –

回答

1

我不确定你为什么会得到你的错误。但是,我能够根据您提供的信息编写一个可正常工作的程序。看看下面的内容。希望它能帮助你确切地发现你在做什么不同。 请注意,我的ToString()实现使用了C#6中的新功能。如果您使用的是C#的较旧版本,则必须稍微修改该行。

using PagedList; 
using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace ConsoleApplication1 
{ 
    class Actor 
    { 
     public string Title { get; set; } 
     public List<string> Actors { get; set; } 
     public override string ToString() 
     { 
      return $"{Title} featuring {string.Join(", ", Actors)}"; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Actor> movies = new List<Actor>() 
      { 
       new Actor() { Title = "Star Wars", Actors = new List<string>() { "Mark Hamill", "Harrison Ford" } }, 
       new Actor() { Title = "Indiana Jones and The Raiders of the Lost Ark", Actors = new List<string>() { "Karen Allen", "Harrison Ford" } }, 
       new Actor() { Title = "Indiana Jones and The Temple of Doom", Actors = new List<string>() { "Kate Kapshaw", "Harrison Ford" } }, 
       new Actor() { Title = "Indiana Jones and The Kingdom of the Crystal Skull", Actors = new List<string>() { "Cate Blanchett", "Harrison Ford" } }, 
       new Actor() { Title = "Jessica Jones", Actors = new List<string>() { "Krysten Riter", "David Tennant" } }, 
       new Actor() { Title = "The Wizard of Oz", Actors = new List<string>() { "Judy Garland" } }, 
      }; 

      var list = movies.Where(x => x.Title.Contains("Jones")).Union(movies.Where(x => x.Actors.Contains("Harrison"))); 

      var pagedList = list.ToPagedList(1, 2); 

      foreach (var movie in pagedList) 
      { 
       Console.WriteLine(movie); 
      } 
     } 
    } 
} 
1

有一个选择的过载,可让您的序号,因此,或许你可以这样做:

list1.Where(a).OrderBy(x => x.Something) 
    .Union(list1.Where(x).OrderBy(x => x.SomethingDifferent)) 
    .Select((item,idx) => new { item, idx }) 
    .OrderBy(x => x.idx); 

不幸的是,这意味着你没有像以前那样选择同一类型。

.Select((item,idx) => new { item.Title, item.SomeOtherProperty, idx }) 

其中如果无论是消费它可以回避型,可能

+0

有趣的是,这个问题是22个小时,我们都在几秒钟内回答对方。 –

相关问题