linqtosql将本地列表连接到表 - 混乱[英] LinqToSQL joining local list to table - confusion

本文是小编为大家收集整理的关于linqtosql将本地列表连接到表 - 混乱的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用linqtosql开发一个应用程序.作为其中的一部分,我创建了一个整数列表,该列表代表我要过滤的密钥.过去,我做过这件事并尝试加入我的列表和数据表,我都会收到以下错误:

本地序列不能在LINQ中用于SQL实现查询运算符,除了contains()operator

现在这很好,因为据我了解,这是Linqtosql的限制/功能.我一直在使用包含操作员进行查询,如下所示:

List<CargoProduct> cargoProducts = context.CargoProducts
                                   .Where(cp => cargos.Contains(cp.CargoID))
                                    .ToList();

最近,我遇到了2100个项目的限制,因此正在寻找其他方法,最终提出以下内容:

List<CargoProduct> cargoProducts = context.CargoProducts.AsEnumerable()
                                   .Join(cargos, cp => cp.CargoID, c => c, (cp, c) => cp)
                                   .ToList();

现在,这正常工作,因此我正在为其他开发人员遇到此限制,为其他开发人员提供一封知识共享电子邮件.我试图收到错误消息,因此将另一个查询汇总到我期望失败的情况下:

List<CargoProduct> results = (from c in cargos
                              join cp in context.CargoProducts on c equals cp.CargoID
                              select cp).ToList();

令我惊讶的是,这不仅没有丢失错误,而且返回的结果与以前的查询完全相同.那么,我在这里想念什么?我确定这是明显的!

参考上下文是我的linqtosql连接,而cargos的实例为:

List<int> cargos = context.Cargos.Select(c => c.CargoID).ToList();

更新

正如答复中提到的那样,确实似乎是我加入内容的顺序,好像我使用以下内容,然后收到预期的错误消息:

List<CargoProduct> test3 = (from cp in context.CargoProducts
             join c in cargos on cp.CargoID equals c
             select cp).ToList();

这是有趣的功能,我想我知道为什么它在做它的工作.可能是一个很好的解决方法,而不是为较小的数据集使用包含.

推荐答案

在此查询中

List<CargoProduct> results = (from c in cargos
                         join cp in context.CargoProducts on c equals cp.CargoID
                         select cp).ToList();

JOIN语句中的左操作数为IEnumerable类型,然后在方法过载分辨率上选择了Enumerable.Join扩展方法.这意味着将整个碳生产表加载到内存中,并通过LINQ过滤到对象.它类似于做context.CargoProducts.AsEnumerable().

本文地址:https://www.itbaoku.cn/post/1557045.html

问题描述

I am developing an application using LinqToSQL. As part of this I create a list of integers, which represent keys I want to filter. Every time in the past that I've done this and tried to join my list and the data table I get the following error:

Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator

Now this is fine because, as I understand it, it is a limitaiton/feature of LinqToSQl. I've been using the Contains operator for my queries as shown:

List<CargoProduct> cargoProducts = context.CargoProducts
                                   .Where(cp => cargos.Contains(cp.CargoID))
                                    .ToList();

Recently I've come across the 2100 item limitation in Contains, so was looking for other ways to do it, eventually coming up with the following:

List<CargoProduct> cargoProducts = context.CargoProducts.AsEnumerable()
                                   .Join(cargos, cp => cp.CargoID, c => c, (cp, c) => cp)
                                   .ToList();

Now, that works fine so I was putting together a knowledge sharing email for the other developers in case they came across this limitation. I was trying to get the error message so put together another query than I'd expect to fail:

List<CargoProduct> results = (from c in cargos
                              join cp in context.CargoProducts on c equals cp.CargoID
                              select cp).ToList();

Much to my surprise, not only did this not throw an error but it returned exactly the same results as the previous query. So, what am I missing here? I'm sure it's something obvious!

For reference context is my LinqToSQl connection and cargos is instantiated as:

List<int> cargos = context.Cargos.Select(c => c.CargoID).ToList();

Update

As mentioned in the reply it would indeed appear to be the order in which I am joining stuff, as if I use the following then I get the expected error message:

List<CargoProduct> test3 = (from cp in context.CargoProducts
             join c in cargos on cp.CargoID equals c
             select cp).ToList();

It's interesting functionality and I think I understand why it is doing what it does. Could be a good workaround instead of using Contains for smaller datasets.

推荐答案

In this query

List<CargoProduct> results = (from c in cargos
                         join cp in context.CargoProducts on c equals cp.CargoID
                         select cp).ToList();

the left operand in the join statement is of type IEnumerable, then the Enumerable.Join extension method is being chosen on method overload resolution. This means that the whole CargoProducts table is being loaded in memory and and filtered via Linq To Objects. It is similar to do context.CargoProducts.AsEnumerable().