C#LINQ返回从LINQ多维数组 [英] c# linq return a multidimensional array from linq
问题描述
我要回多维数组在会议期间举办,但不知道如何从LINQ返回它:
公共字符串[] GetCountryAndManufacturerForUser(INT用户id)
{ 变种阵列=(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择新的{xy.Name,xz.Description})ToArray的()。 返回??
}
我知道我为在这里做得不对只是不知道是什么。
编辑:
以下字段需要退回 - xy.Name,xz.Description
这样的:
{1,AAA},
{2,BBB}
编辑:
我曾尝试下面的例子,他们的避风港牛逼完全得到的地方,我需要的是 - 我想类似下面应该工作:
///<总结>
///
///< /总结>
///< PARAM NAME =用户id>< /参数>
///<&回报GT;< /回报>
公共字符串[,] GetCountryAndManufacturerForUser(INT用户id)
{ 变种阵列=(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择新的{xy.Name,xz.Description})ToArray的()。 返回数组; }
但是,抱怨返回的数组;
编辑:
我已经得到的最接近如下:
///<总结>
///
///< /总结>
///< PARAM NAME =用户id>< /参数>
///<&回报GT;< /回报>
公共字符串[] [] GetCountryAndManufacturerForUser(INT用户id)
{ 在_er.UserRoles // VAR阵列=(从XX
//在_er.Countries加入XY上xx.CountryId等于xy.Id
//在_er.Manufacturers加入XZ上xx.ManufacturerId等于xz.Id
//其中xx.UserId ==用户id
//选择新的{xy.Name,xz.Description})ToArray的()。 变种countryArray =(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择xy.Name).ToArray(); 变种manufacturerArray =(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择xz.Description).ToArray(); //返回数组;
返回新的字符串[] [] {countryArray,manufacturerArray};
}
但是这两个返回的数组:
VAR userManuCountry = _userRoleRepository.GetCountryAndManufacturerForUser(u.Id); userManuCountry {字符串[2] []}的String [] []
[0] {字符串[6]}的String []
[0]德国字符串
[1]法国的字符串
[2]联合王国的字符串
[3]荷兰的字符串
[4]美国串
[5]联合王国的字符串
- [1] {字符串[6]}的String []
[0]鸽的字符串
[1]鸽的字符串
[2]鸽的字符串
[3]鸽的字符串
[4]鸽的字符串
[5]当然字符串
铁血阵列。
公共字符串[] [] GetCountryAndManufacturerForUser(INT用户id)
{ 返回(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择新的String [] {xy.Name,xz.Description})ToArray的()。
}
多维数组
公共字符串[,] GetCountryAndManufacturerForUser(INT用户id)
{ 变种阵列=(从_er.UserRoles XX
加入XY在_er.Countries上xx.CountryId等于xy.Id
加入XZ在_er.Manufacturers上xx.ManufacturerId等于xz.Id
其中,xx.UserId ==用户id
选择新的List<字符串方式> {xy.Name,xz.Description})ToArray的();
返回CreateRectangularArray(数组);}
静态T [,] CreateRectangularArray< T>(IList的< T> []数组)
{
// TODO:确认和特殊外壳为arrays.Count == 0
INT minorLength =阵列[0] .Count之间();
T [,] t RET =新的T [arrays.Length,minorLength]
的for(int i = 0; I< arrays.Length;我++)
{
VAR阵列=阵列[我]
如果(array.Count!= minorLength)
{
抛出新的ArgumentException
(所有阵列必须是相同的长度);
}
对于(INT J = 0; J< minorLength; J ++)
{
RET [I,J] =阵列[J]。
}
}
返回RET;
}
从以上答案之一约翰飞碟双向的问题的如何数组列表转换成二维数组
I want to return a multidimensional array to hold in a session but not sure how to return it from 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??
}
i know i m doing something wrong here just not sure what.
Edit:
The following fields need to be returned - xy.Name, xz.Description
like:
{ "1", "aaa" },
{ "2", "bbb" }
Edit:
I have tried the example below and they haven t quite got to where i need to be - i thought something like the following should work:
/// <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;
}
But it complains about return array;
Edit:
The closest i ve got is the following:
/// <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 };
}
but this return two array :
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
Jagged array.
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();
}
Multidimensional array
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;
}
Above method taken from one of answer by John skeet for the Question How to convert list of arrays into a 2D array
这篇关于C#LINQ返回从LINQ多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!