新的字符串列表在LINQ中初始化时重新排列列表元素的顺序 [英] New string list shuffles order of list elements on initialization in LINQ

查看:86
本文介绍了新的字符串列表在LINQ中初始化时重新排列列表元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对ASP.NET 5 API项目感到沮丧之后,我决定将其重建为一个较旧的WebApi 2项目.我正在尝试从LINQ查询中生成2个字符串的集合的列表(最初是2个元素的字符串数组的列表,现在是字符串列表的列表).

After running into some frustrations with my ASP.NET 5 API project I decided to rebuild it as an older WebApi 2 project. I'm trying to generate a list of collections of 2 strings (originally a list of a 2 element string array, now a list of list of strings) from a LINQ query.

这是我希望在ASP.NET 5项目中使用的伪代码:

Here is the pseudocode that worked as I wanted it to in the ASP.NET 5 project:

var testList = db.MyTable.Where(x => [WHERE Clauses]).Select(x => new string[] { x.Number.Trim(), x.Name.Trim() }).ToList();

该新项目在查询中被阻塞,理由是它出于某种原因不喜欢使用字符串数组(我假设EF 6与7有所不同?).作为一种解决方法,我改为让查询返回一个字符串列表列表,但该查询混合了它返回的"Number"和"Name"字段的顺序,有时Number是第一个元素,而其他时候Name是.这是我尝试过的所有新查询代码尝试,所有尝试均以混乱的元素顺序结束:

The new project choked on the query citing it didn't like using string arrays for whatever reason (I'm assuming difference in EF 6 vs 7?). As a fix I instead had the query return a list of string lists but it mixes up the order of the "Number" and "Name" fields it returns, sometimes the Number is the first element and other times the Name is. Here is some new query code attempts I tried that all ended up with the jumbled element order:

var testList = db.MyTable.Where(x => [WHERE Clauses]).Select(x => new List<string> { x.Number.Trim(), x.Name.Trim() }).ToList();
var testList = db.MyTable.Where(x => [WHERE Clauses]).Select(x => (new string[2] { x.Number.Trim(), x.Name.Trim() }).ToList()).ToList();
var testList = db.MyTable.Where(x => [WHERE Clauses]).Select(x => new List<string>(new string[] { x.Number.Trim(), x.Name.Trim() })).ToList();

我意识到有很多方法可以获取最终结果,但我希望有人可以帮助我理解为什么这些列表元素会以不同的顺序放置在列表中(有时是名称",然后是数字")其他时间是数字,然后是名称),即使我是基于一个新创建的数组生成一个列表,该数组以前也完全保留了顺序.

I realize there are a ton of different ways to get the end result I'm looking for but I'm hoping someone can help me understand why these list elements would be placed in the list in differing orders (sometimes Name then Number other times Number then Name), even when I generate a list based on a newly created array which preserved the order perfectly previously.

推荐答案

我很想知道为这些查询生成什么SQL,以及当结果更改时SQL是否更改.在我看来,选择列表似乎很少见,因此您可能遇到了极端情况(或至少遇到了未定义的行为领域).

I'd be curious to see what SQL is generated for those queries and whether or not it changes when your result changes. Selecting a List to me seems like a pretty rare situation, so it's possible you have run into an edge-case (or at least into undefined behavior territory).

由于您实际上仍然只是选择两列,因此我们可以担心在Entity Framework完成工作后数据的结构

Since you're still essentially just selecting two columns, we can worry about how the data is structured after Entity Framework has finished it's work

db.MyTable.Where(x => [WHERE Clauses])
    .Select(x => new { Number = x.Number.Trim(), Name = x.Name.Trim() })
    .AsEnumerable() //Force EF to materialize the result here
    .Select(x => new List<string> { x.Number, x.Name }) //Manipulate the result in memory
    .ToList();

这篇关于新的字符串列表在LINQ中初始化时重新排列列表元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆