gpt4 book ai didi

c# - 在 linq to sql 中使用 Select New List<> 时所有元素的列表的相同实例

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:45 25 4
gpt4 key购买 nike

在 linq to sql 中使用 Select New List<> 时所有元素的列表的相同实例

我最近遇到了一个非常奇怪的行为,我需要一些帮助来澄清。我有一个解决该问题的方法,但不明白为什么它首先会发生并怀疑它是 LINQ to SQL 的错误或者我理解错误。

我遇到的问题是 linq to sql 语句中的 select 子句。

考虑以下代码,其中 db.MessageStatusTypes 是一个“表”。我在我的自定义数据库中使用了一个表,但只要表中至少有 2 行,使用哪个表似乎并不重要。

List<List<string>> construct = (from o in db.MessageStatusTypes
select new List<string>() { "Hello" }
).ToList();
construct[0].Add("World");
return construct[1].Count();

我期望的是构造是 X 新列表的列表。其中 X 是数据库中的行数。

并且如果我修改了其中一个列表,则其他列表不会被修改。但事实并非如此。在上面的示例中,如果我将字符串 World 添加到第一个数组,它会添加到数组的所有 X。

与不使用 Linq to SQL 的相同代码对比

List<int> iList = new List<int>() { 1, 2, 3, 4 };
List<List<string>> construct = (from i in iList
select new List<string>() { "Hello" }
).ToList();
construct[0].Add("World");
return construct[1].Count();

在这种情况下,当我将世界添加到第一个列表时,其他列表不会受到影响。

您能否解释一下为什么我们会出现不同的行为,以及 LINQ to SQL 是否存在已知或未知的错误?

   using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp {
class Program {


static void Main(string[] args) {
int count1 = function1(); //return 1
int count2 = function2(); //return 2?


}


static int function1() {
List<int> iList = new List<int>() { 1, 2, 3, 4 };
List<List<string>> construct = (from i in iList
select new List<string>() { "Hello" }
).ToList();
construct[0].Add("World");
return construct[1].Count();
}

static int function2() {
Data.DBManager dbManager = new Data.DBManager(throwExceptionOnLocal: false);
string connectionString = ConfigurationManager.ConnectionStrings["Data.Properties.Settings .FactoryCommunicationsConnectionString"].ConnectionString;
using (Data.FactoryCommunicationsContext db = new Data.FactoryCommunicationsContext(connectionString)) {

List<List<string>> construct = (from o in db.MessageStatusTypes
select new List<string>() { "Hello" }
).ToList();
construct[0].Add("World");
return construct[1].Count();
}
}


}



}

最佳答案

我向 Microsoft 提出了一个问题,他们确认这是预期的行为,而不是错误。

它归结为以下内容:

  1. Linq to Objects 和 Linq2SQL 完全不同。引用 Linq2SQL 开发者的话——“首先,linq2sql 与 linq to object 无关。 IQueryProvider 对象决定如何执行 IQueryable 查询。显然,他们使用不同的 IQueryProvider。

  2. Linq2Sql 处于仅维护模式。 Microsoft 的回应:“我鼓励您研究使用 Entity Framework 而不是原始的 LINQ to SQL,以获得 EF 提供的更大灵 active 。我很确定 Microsoft 不再对 LinqToSQL 的增强功能进行任何投资,目前它处于严格的维护模式。即使我们确认这是一个错误,批准修复的阈值也很高。 “

  3. 当翻译包含列表的 select 子句时,LINQ to SQL 不能很好地解释您的意思。

考虑以下语句:

List<List<string>> construct = (from o in db.MessageStatusTypes
select new List<string>() { "Hello" }).ToList();

这是微软的回应

“Linq2sql将linq表达式转换为SqlExpression,并根据那些SqlExpressions生成sql。这是陷阱。 [new List() { "Hello"}] 被认为是一个 ConstantExpression(这是由编译器完成的)并被转换为 SqlValue 表达式。因此它只执行一次。”

因为它只会在我们遇到这种非常奇怪的行为时执行,即为每个结果分配相同的列表。

现在考虑其他一些非常相似的语句

List<List<string>> construct = (from o in db.MessageStatusTypes select
generateList() ).ToList();

static List<string> generateList() {
if (DateTime.Now.Ticks % 2 == 0) {
return new List<string> { "Hello" };
}
else {
return new List<string> { "Hello" };
}
}

这会返回完全相同的东西,因为 generateList() 也被认为是一个常量表达式。 generateList() 只会被调用一次。关键是当编译器认为它根本不依赖于从 SQL 返回的数据时,它会将其视为常量表达式。

List<List<string>> construct = (from o in db.MessageStatusTypes
select new List<string>() { o.Description}).ToList();

这句话看起来应该有效。因为它现在取决于返回的数据。但是,我们遇到了另一个问题(运行时异常 - 无法创建类型为“System.Collections.Generic.List`1[System.String]”的查询结果)。 LINQ2SQL 不支持这个。

以下是 Linq2SQL 开发者的官方回复:

“这种不同的行为是由linq表达式和linq2sql表达式之间的转换引起的。 [new List() { o.Description}] 转换为仅支持 CLR Array 类型的 sqlClientArray 表达式。”

这最终将我们带到了解决方法。

List<List<string>> construct = (from o in db.MessageStatusTypes select
generateList(o.Description) ).ToList();

static List<string> generateList(string description) {
return new List<string> { “Hello” };
}

这将给我们预期的结果,这是来自 Linq2SQL 开发人员的原因:

“这是 methodcall 表达式,转换为 SqlMethodCall 表达式,linq2sql 通过该表达式动态生成 IL”

评论中提到的另一种解决方法是避免在 Linq2SQL 语句中创建列表。相反,首先获取数据,然后使用 Linq to Object 语句创建包含列表的所需对象。

关于c# - 在 linq to sql 中使用 Select New List<> 时所有元素的列表的相同实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com