C# c linq从linq返回多维数组
我想返回要在会话中保留的多维数组,但不确定如何从linq返回它:C# c linq从linq返回多维数组,c#,arrays,linq,multidimensional-array,C#,Arrays,Linq,Multidimensional Array,我想返回要在会话中保留的多维数组,但不确定如何从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
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
比如:
编辑:
我已经尝试了下面的例子,但他们还没有完全达到我需要的程度-我认为下面的方法应该有效:
/// <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;
}
锯齿阵列
多维数组
上述方法摘自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;
}
原因是,如果您添加第三个或更多字段,使用字符串[]的代码将中断。string[]是一维数组。你的方法的返回类型是错误的吗?我必须说,我们应该只使用多维数组进行矩阵计算。否则,请改用嵌套列表。查询的可能重复项将返回具有两个属性的匿名对象,但方法应返回字符串数组。您需要将匿名对象转换为stringsSome挑剔:这不是一个多维数组,而是一个交错数组。使用上面的方法,我得到的数组类型“System.String[]”无法在查询结果中初始化。考虑使用Stry.Stopy.Eng.List.1(Strase.String)代替.@ DaMITE最后一件事是,重新排列后,数组有时会被混淆,而不是返回选择新的列表{xz。描述,XY,Name },ToArray;它带回了描述名称描述,而不是描述名称描述名称等我不确定原因-如果我在sql中运行查询,我看到了我期望的描述-描述-名称与Damith示例它失败,说我需要使用列表用户2795707你能提供一个示例吗?例如+1给出一个解决方案,避免出现可避免的损坏,如字符串[][]一个。请原谅语法错误。即使有详细的注释,过滤器也不允许将+1作为第一个词,但不要将+1放在第一个词之后。为什么不加一表示给出…:-
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 ;
}
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;
}
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 ;
}