当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《c#linq从linq返回一个多维数组》经验,为你挑选了1个好方法。

我想返回一个多维数组来保存在一个会话中,但不知道如何从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" }

编辑:

我已经尝试了下面的例子,他们还没有到达我需要的地方 - 我认为以下内容应该有效:

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

    }

但它抱怨返回阵列;

编辑:

我最接近的是以下内容:

/// 
        /// 
        /// 
        /// 
        /// 
        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

Damith.. 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{ xy.Name, xz.Description }).ToArray(); 
    return CreateRectangularArray(array);  

}


static T[,] CreateRectangularArray(IList[] 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> Damith..:

锯齿状阵列.

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{ xy.Name, xz.Description }).ToArray(); 
    return CreateRectangularArray(array);  

}


static T[,] CreateRectangularArray(IList[] 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对于如何将数组列表转换为多维数组的问题

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有