gpt4 book ai didi

c# - Linq 数组列表顺序更改

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

您好,我有一个数组,可以获取制造商和国家/地区,由于某些原因,当数组返回时,数组的顺序有时会发生变化。

这是 Linq 查询:

  var array = (from xx in _er.UserRoles
join xy in _er.Countries on xx.CountryId equals xy.Id
join xz in _er.Manufacturers on xx.ManufacturerId equals xz.Id
where xx.UserId == userId
select new List<string> { xz.Description, xy.Name }).ToArray();

地点:xz.Description 是制造商xy.Name 是国家

在我的数组中,我希望得到以下内容:

[0]    Count = 2
[0] Dove
[1] Uk
[1] Count = 2
[0] Dove
[1] France
[2] Count = 2
[0] Sure
[1] UK
...

但在某些情况下,我会得到以下信息:

[0]    Count = 2
[0] Dove
[1] Uk
[1] Count = 2
[0] France
[1] Dove
[2] Count = 2
[0] UK
[1] Sure
...

当我在数据库中运行查询以检查每个制造商都有一个国家时,我最初认为可能是那个国家。

谁能就为什么会发生这种情况提出建议?

编辑

这是 sql 查询和一些示例数据:

select m.Description, c.Name from UserRoles ur
join Countries c on ur.CountryId = c.Id
join Manufacturers m on ur.ManufacturerId = m.Id
where ur.userid = 435

示例数据:

Description     Name
Lynx United Kingdom
Persil United Kingdom
Dove Brazil
Dove Canada
Dove Germany
Dove France
Dove United Kingdom
Dove Netherlands
Dove United States
Surf United Kingdom
Comfort United Kingdom
Sure United Kingdom
Bertolli United Kingdom
Bertolli United States

编辑2

下面是对我正在做的事情的更多解释,这样可以更多地解释我最终需要什么:

在我的 Controller 中,我将数组放入 session 中:

Controller 代码:

  var userManuCountry = _userRoleRepository.GetCountryAndManufacturerForUser(u.Id);
Session["userManuCountry"] = userManuCountry;

存储库代码:

/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public string[,] GetCountryAndManufacturerForUser(int userId)
{

var array = (from xx in _er.UserRoles
join xy in _er.Countries on xx.CountryId equals xy.Id
join xz in _er.Manufacturers on xx.ManufacturerId equals xz.Id
where xx.UserId == userId
select new List<string> { xz.Description, xy.Name }).ToArray();
return CreateRectangularArray(array);

}


static T[,] CreateRectangularArray<T>(IList<T>[] arrays)
{
// TODO: Validation and special-casing for arrays.Count == 0
int minorLength = arrays[0].Count();
T[,] ret = new T[arrays.Length, minorLength];
for (int i = 0; i < arrays.Length; i++)
{
var array = arrays[i];
if (array.Count != minorLength)
{
throw new ArgumentException
("All arrays must be the same length");
}
for (int j = 0; j < minorLength; j++)
{
ret[i, j] = array[j];
}
}
return ret;
}

另一个 Controller - 我正在使用 session 列出制造商的国家/地区:

 /// <summary>
/// et the specific countries for user and manufacturer
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Get)]
// [ValidateAntiForgeryToken]
// [Authorize(Roles = "ReportingDashboardAccess")]
public ActionResult GetListOfCountriesForUserManufacturer(int userId, string manu)
{
manu = manu.Trim();
// get the specific countries for user and manufacturer
var countries = new List<string>();

//here we want to use the manu to get the countries from seesion rather than db - this is a multidimensional array
string[,] manuCountry = (string[,])Session["userManuCountry"];

var addCountry = false;
//loop through to find countries for each manufacturer
for (int row = 0; row < manuCountry.GetLength(0); row++)
{
for (int col = 0; col < manuCountry.GetLength(1); col++)
{
string result = manuCountry[row, col];
result.Trim();
if (addCountry == true && col == 1)
{
//addcountry has been set to true so add it
countries.Add(result);
addCountry = false;
}
else if (addCountry == true && col == 0)
{
addCountry = false;
}
if (result == manu)
{
//the next one that comes through is the country
addCountry = true;

}


}
}

countries.Sort();
ViewData["allCountries"] = new SelectList(countries);


return View("CountriesParam");

}

非常感谢!

最佳答案

集合初始化器 ( new List<string> { xz.Description, xy.Name } ) 应该保留表达式中指定的项目顺序,因此您的代码应该可以工作。

我猜想有一些东西在创建的列表上运行并以某种方式混淆了排序。

也就是说,对具有不同含义的值使用列表(或任何集合)是不直观的。即使它们都是字符串,这些值也不具有相同的上下文。给他们明确不同的容器会好得多。例如现在,如果您用数据填充几个文本框,您将使用:

txtName.Text = list[0];
txtCountry.Text = list[1];

而且错误很难被发现和诊断。如果您使用

将结果放在单独的实体(例如匿名类)中
select new { Name = xz.Description, Country = xy.Name }

你可以用

txtName.Text = myObject.Name;
txtCountry.Text = myObject.Country

关于 edit2:如果我正确理解您的情况,您需要的是获取制造商允许的国家/地区列表。 Dictionary<string, IEnumerable<string>> 是此类数据的理想容器。 , 而不是 string[,] .

我会像这样重构 LINQ:

//gets the data from the database
var data = (from xx in _er.UserRoles
join xy in _er.Countries on xx.CountryId equals xy.Id
join xz in _er.Manufacturers on xx.ManufacturerId equals xz.Id
where xx.UserId == userId
select new { Name = xz.Description, Country = xy.Name });
//formats the data into a dictionary
var result = data.GroupBy(a => a.Name)
.ToDictionary(// the name of the product
g => g.Key,
// the list of countries for the product
g => g.Select(a => a.Country).ToList());
return result;

然后像这样使用它(在 GetListOfCountriesForUserManufacturer 中):

public ActionResult GetListOfCountriesForUserManufacturer(int userId, string manu)
{
manu = manu.Trim();

//I'm not too crazy about sesiion usage, but that's a whole other issue
var manuCountry = (Dictionary<string, List<string>>)Session["userManuCountry"];

// get the specific countries for user and manufacturer
var countries = manuCountry[manu];
countries.Sort();
ViewData["allCountries"] = new SelectList(countries);
return View("CountriesParam");
}

关于c# - Linq 数组列表顺序更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999024/

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