5

セッションで保持する多次元配列を返したいのですが、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 };
    }

しかし、これは2つの配列を返します:

 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

4 に答える 4

12

ジャグ配列。

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 の回答の 1 つから取られたものです。

于 2013-09-19T13:40:54.570 に答える
2

ベストプラクティスは、定義されたメンバーで List<> を使用することです

public class Results
{
    public string name {get; set;}
    public string description {get; set;}
}
于 2013-09-19T13:43:58.640 に答える
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 番目のフィールド (またはそれ以上) を追加すると、string[][] を消費するコードが壊れてしまうからです。

于 2013-09-19T13:44:29.030 に答える
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 ;
    }
于 2013-09-19T13:56:43.073 に答える