我想返回一个多维数组以保存在会话中,但不知道如何从 linq 返回它:
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 { xy.Name, xz.Description }).ToArray();
return??
}
我知道我在这里做错了什么只是不确定是什么。
编辑:
需要返回以下字段 - xy.Name, xz.Description
喜欢:
{ "1", "aaa" },
{ "2", "bbb" }
编辑:
我已经尝试了下面的示例,但他们还没有完全到达我需要的地方 - 我认为类似以下的东西应该可以工作:
/// <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 { xy.Name, xz.Description }).ToArray();
return array;
}
但它抱怨返回数组;
编辑:
我得到的最接近的是以下内容:
/// <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 { xy.Name, xz.Description }).ToArray();
var countryArray = (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 xy.Name).ToArray();
var manufacturerArray = (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 xz.Description).ToArray();
// return array;
return new string[][] { countryArray, manufacturerArray };
}
但这返回两个数组:
var userManuCountry = _userRoleRepository.GetCountryAndManufacturerForUser(u.Id);
userManuCountry {string[2][]} string[][]
[0] {string[6]} string[]
[0] "Germany" string
[1] "France" string
[2] "United Kingdom" string
[3] "Netherlands" string
[4] "United States" string
[5] "United Kingdom" string
- [1] {string[6]} string[]
[0] "Dove" string
[1] "Dove" string
[2] "Dove" string
[3] "Dove" string
[4] "Dove" string
[5] "Sure" string