Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# linq return a multidimensional array from linq

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 multidimensional array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...