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

但这返回两个数组:

 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 对问题如何将数组列表转换为多维数组的回答之一

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

原因是如果您添加第三个字段(或更多),使用您的 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 回答