c#linq从linq返回一个多维数组

时间:2013-09-19 13:35:42

标签: 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 _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

4 个答案:

答案 0 :(得分:11)

锯齿状阵列。

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;
}

以上方法取自约翰双向飞碟对问题How to convert list of arrays into a multidimensional array

的回答

答案 1 :(得分:2)

最佳做法是使用List&lt;&gt;已定义的成员

public class Results
{
    public string name {get; set;}
    public string description {get; set;}
}

答案 2 :(得分:2)

个人喜好,但我想我会沿着这条路走下去

    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;
    }

原因是,如果您添加第三个字段(或更多字段),则使用字符串[] []的代码将会中断。

答案 3 :(得分:0)

    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 ;
    }