如何成功加入两个内存中的linq集合?

时间:2023-02-10 02:31:19

I'm trying to join one Linq collection from Data Base and one from XML file. Is this possible? I always get: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.

我正在尝试从Data Base加入一个Linq集合,从XML文件加入一个。这可能吗?我总是得到:除了Contains()运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现。

Here is my code:

这是我的代码:

MyDataContext dc = new MyDataContext();
XElement CustomData;

var pages = from p in dc.Pages
            select new
            {
                Title = p.Title,
                Slug = p.Slug,
                PageId = p.PageId.ToString()
            };

var orders = from p in CustomData.Element("pages").Elements("page")
             select new
             {
                 PageId = (string)p.Attribute("id"),
                 SortOrder = (int)p.Attribute("sortOrder")
             };


var source = from p in pages
             join o in orders
             on p.PageId equals o.PageId
             select p;

source.ToList();

2 个解决方案

#1


I don't think you need to do the join at all.

我认为你根本不需要加入。

MyDataContext dc = new MyDataContext();
XElement CustomData;

var orders = CustomData.Element("pages").Elements("page")
                       .Select( o =>  new
                         {
                             PageId = p.Attribute("id").ToString(),
                             SortOrder = (int)p.Attribute("sortOrder")
                         });

var source = dc.Pages
               .Where( p => orders.Select( o => o.PageId)
                                  .Contains( p.PageId.ToString() ))
               .Select( p => new
                {
                  Title = p.Title,
                  Slug = p.Slug,
                  PageId = p.PageId.ToString()
                });

#2


It doesn't look like you can do the join between the local collection (orders) and the LINQ2SQL results with deferred execution. You can execute the pages query ToList (like tvanfosson suggested originally:)) or maybe do something like this...

看起来你不能在本地集合(订单)和延迟执行的LINQ2SQL结果之间进行连接。您可以执行页面查询ToList(如tvanfosson最初建议:))或者可能做这样的事情......

var source = from p in pages
             where orders.Select(o=> o.PageID).Contains(p.PageID)
             select p;

It's not quite the join you were looking for, but if you want the deferred execution of LINQ2SQL you can have it this way.

它不是你想要的连接,但如果你想延迟执行LINQ2SQL,你可以这样做。

#1


I don't think you need to do the join at all.

我认为你根本不需要加入。

MyDataContext dc = new MyDataContext();
XElement CustomData;

var orders = CustomData.Element("pages").Elements("page")
                       .Select( o =>  new
                         {
                             PageId = p.Attribute("id").ToString(),
                             SortOrder = (int)p.Attribute("sortOrder")
                         });

var source = dc.Pages
               .Where( p => orders.Select( o => o.PageId)
                                  .Contains( p.PageId.ToString() ))
               .Select( p => new
                {
                  Title = p.Title,
                  Slug = p.Slug,
                  PageId = p.PageId.ToString()
                });

#2


It doesn't look like you can do the join between the local collection (orders) and the LINQ2SQL results with deferred execution. You can execute the pages query ToList (like tvanfosson suggested originally:)) or maybe do something like this...

看起来你不能在本地集合(订单)和延迟执行的LINQ2SQL结果之间进行连接。您可以执行页面查询ToList(如tvanfosson最初建议:))或者可能做这样的事情......

var source = from p in pages
             where orders.Select(o=> o.PageID).Contains(p.PageID)
             select p;

It's not quite the join you were looking for, but if you want the deferred execution of LINQ2SQL you can have it this way.

它不是你想要的连接,但如果你想延迟执行LINQ2SQL,你可以这样做。