C# LINQ 从 LINQ 返回一个多维数组
本文关键字:LINQ 一个 数组 返回 | 更新日期: 2023-09-27 18:31:54
我想返回一个多维数组以保存在会话中,但不确定如何从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。名称,xz。描述
喜欢:
{ "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
交错数组。
public string[][] GetCountryAndManufacturerForUser(int userId)
{
return (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 string[]{ xy.Name, xz.Description }).ToArray();
}
多维数组
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>{ xy.Name, xz.Description }).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;
}
以上方法取自John Skeet对问题"如何将数组列表转换为多维数组"的回答之一
最佳做法是使用具有已定义成员的列表<>
public class Results
{
public string name {get; set;}
public string description {get; set;}
}
个人喜好,但我想我会走这条路
public class UserData
{
public string Name;
public string Description;
}
public UserData[] 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 UserData() { xy.Name, xz.Description }).ToArray();
return array;
}
原因是,如果您添加第三个(或更多)字段,则使用字符串[][]的代码将中断。
public class UserData
{
public string Name;
public string Description;
}
public List<UserData> GetCountryAndManufacturerForUser(int userId) {
List<UserData> userdatas = new List<UserData>;
userdatas = (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 = xy.Name,
Description = xz.Description
}).ToList();
return userdatas ;
}