Linq2SQL .Union()抛出'本地序列无法使用...'错误

时间:2012-03-04 00:15:14

标签: c# windows-phone-7 linq-to-sql silverlight-toolkit

我目前正在更新一个Windows Phone应用程序,显然是在Silverlight For Windows Phone中编写的。 我试图联合两组对象,但我收到以下错误

  

除了Contains运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现。

我已经读过无法加入本地数组和表格,但是我只是在我的代码中无法实现这一点......坦白说,我甚至不确定这是不是导致我的问题。

这是我目前的代码:

    //Creating a 'blank' group of movies objects
    List<String> characters = new List<String> { "#", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
    List<Group<Movies>> emptyGroups = new List<Group<Movies>>();
    characters.ForEach(x => emptyGroups.Add(new Group<Movies>(x, new List<Movies>())));

    //Querying the database
    var allMovies = from m in dataContext.MoviesTable
                            orderby m.Title
                            select m;

    //Grouping all movie objects into a container of Group(string name, IEnumerable<T> items)
    var groupedMovies = (from t in allMovies
                         group t by t.Title.ToLower()[0].ToString() into grp
                         orderby grp.Key
                         select new Group<Movies>(grp.Key, grp));

    // This is where everything falls over.
    IEnumerable LongList.ItemsSource = (from t in groupedMovies.Union(emptyGroups)
                                              orderby t.Title
                                              select t).ToList();

List<T>'emptyGroups'工作正常,IQueryable<T>'groupedMovies'工作正常。但我无法弄清楚如何将它们组合在一起。

帮助?好吗?

1 个答案:

答案 0 :(得分:1)

您可以AsEnumerable()强制groupsMovies在执行联合之前在SQL中执行,即

IEnumerable LongList.ItemsSource = (
    from t in goupedMovies.AsEnumerable().Union(emptyGroups)
    orderby t.Title
    select t).ToList();