c#linq从linq返回一个多维数组
我想返回一个多维数组来保存在一个会话中,但不知道如何从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; }
但它抱怨返回arrays;
编辑:
我最接近的是以下内容:
/// /// /// /// /// 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
锯齿状arrays。
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; }
以上方法取自约翰双向飞碟的回答问题如何将数组列表转换为多维数组
最佳实践是使用具有已定义成员的List <>
public class Results { public string name {get; set;} public string description {get; set;} }
个人喜好,但我想我会走这条路
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; }
原因是,如果您添加第三个字段(或更多),使用字符串[] []的代码将会中断。
public class UserData { public string Name; public string Description; } public List GetCountryAndManufacturerForUser(int userId) { List userdatas = new List ; 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 ; }