嗨,我有一个包含制造商和国家/地区的数组,由于某种原因,当数组返回时,数组的顺序有时会发生变化。
这是 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
这是对我正在做的事情的更多解释,可能会更多地解释我最终需要什么:
在我的控制器中,我将数组放入会话中:
控制器代码:
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;
}
另一个控制器 - 我正在使用会话列出制造商的国家/地区:
/// <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");
}
非常感谢!