c#数组复制,调整大小

时间:2013-11-23 10:25:57

标签: c# arrays

我有一个数组:

static Array results = new[] { new { Id, RoomCount, Cover, Space, Floor,
                                    FloorCount, NameHouse, Price, NameStreet } };

然后我有第二个:

var res = (//.......//
select new { f.Id, f.RoomCount, f.Cover, f.Space, f.Floor, f.FloorCount,
             h.NameHouse, f.Price, s.NameStreet }).ToArray();

然后我想将res复制到结果:

var lres = res.Length;
res.CopyTo(results, lres);

res的长度变化,结果的长度是固定的,因为它刚刚被宣布。

我有一个例外:dest数组的长度太短。 我还尝试调整dest数组的大小:

var lres = res.Length;
Array.Resize(results, lres);

同样崩溃:方法System.Array.Resize<T>(ref T[], int)的类型参数无法从用法中推断出来。尝试明确指定类型参数。

有什么建议吗?

    public partial class Search : System.Web.UI.Page
    {
    public static int Id { get; set; }
    public static int RoomCount { get; set; }
    public static string Cover { get; set; }
    public static int Space { get; set; }
    public static int Floor { get; set; }
    public static int FloorCount { get; set; }
    public static string NameHouse { get; set; }
    public static decimal Price { get; set; }
    public static string NameStreet { get; set; }

    static Array results = new[] { new { Id, RoomCount, Cover, Space, Floor,      FloorCount, NameHouse, Price, NameStreet } };  
 private Array FormData()
    {

       //some code
        var lng = ht.Length;

        while (lng != 0)
        {
            //
            var var = ht[lng - 1];
            lng--;


            var res = (from f in db.FlatSet
                           .Where(x => x.RoomCount >= rMin && x.RoomCount <= rMax)
                           .Where(x => x.Space >= sMin && x.Space <= sMax)
                           .Where(x => x.Floor >= fMin && x.Floor <= fMax)
                           .Where(x => x.FloorCount >= fcMin && x.FloorCount <= fcMax)
                           .Where(x => x.HouseTypeId == var)// || x.HouseTypeId == 2)
                           .Where(x => x.Price >= pMin && x.Price <= pMax)
                           .Where(x => x.Rent == r)
                       join h in db.HouseTypeSet on f.HouseTypeId equals h.Id
                       join s in db.StreetSet on f.StreetId equals s.Id
                       select new { f.Id, f.RoomCount, f.Cover, f.Space, f.Floor, f.FloorCount, h.NameHouse, f.Price, s.NameStreet }).ToArray();

            var lres = res.Length;
            Array.Resize(ref results, lres);

            res.CopyTo(results, lres);

        }
        return results;
    }


FIXED:
public class result
{
    public int Id { get; set; }
    public int RoomCount { get; set; }
    public string Cover { get; set; }
    public int Space { get; set; }
    public int Floor { get; set; }
    public int FloorCount { get; set; }
    public string NameHouse { get; set; }
    public decimal Price { get; set; }
    public string NameStreet { get; set; }
}
public partial class Search : System.Web.UI.Page
{
    DB_9AB8FB_lisogorEntities db = new DB_9AB8FB_lisogorEntities();


    List<result[]> myList = new List<result[]>();
    List<result> NewMyList = new List<result>();
}
  //////some code
 private List<result[]> FormData()//
    {
 int[] ht = (int[])arr;
        var lng = ht.Length;

        while (lng != 0)
        {
            //some code
            var var = ht[lng - 1];
            lng--;

            myList.Add((from f in db.FlatSet
                           .Where(x => x.RoomCount >= rMin && x.RoomCount <= rMax)
                           .Where(x => x.Space >= sMin && x.Space <= sMax)
                           .Where(x => x.Floor >= fMin && x.Floor <= fMax)
                           .Where(x => x.FloorCount >= fcMin && x.FloorCount <= fcMax)
                           .Where(x => x.HouseTypeId == var)// || x.HouseTypeId == 2)
                           .Where(x => x.Price >= pMin && x.Price <= pMax)
                           .Where(x => x.Rent == r)
                       join h in db.HouseTypeSet on f.HouseTypeId equals h.Id
                       join s in db.StreetSet on f.StreetId equals s.Id
                       select new result{ Id = f.Id, RoomCount = f.RoomCount, Cover = f.Cover, Space = f.Space, Floor = f.Floor, 
                                          FloorCount = f.FloorCount, NameHouse = h.NameHouse, Price = f.Price, NameStreet=s.NameStreet }).ToArray());

        }
        return myList;
    }

    private void BindData()
    {
        var i = myList.Count;

        while (i != 0)
        {
            if (myList[i - 1].Length != 0)
            {
                var j = myList[i - 1].Length;
                while (j != 0)
                {
                    NewMyList.Add(myList[i - 1][j-1]);
                    j--;
                }
            }
            i--;
        }
        Results1.DataSource = NewMyList;
        Results1.DataBind();
    }
    }

3 个答案:

答案 0 :(得分:0)

您正在使用匿名类型,并且cocmpiler无法识别它将如何推断两个数组的并集,因为编译器应该知道要为其创建实例的对象的类型 你在

得到这个例外
 Array.Resize<T> 

应该强类型的方法
只是为了了解更多

 class Program
  {
        static void Main(string[] args)
      {

          Array results = new[] { new { RoomCount = "2", Cover = "5", Space = "5", Floor = "5",    FloorCount = "5", NameHouse = "5", Price = "5", NameStreet = "5" } };            
        //with this line not cpmmented does not compile  
          Array.Resize(ref results, results.Length + 5);            

        String[] myArr = {"The", "quick", "brown", "fox", "jumps", 
        "over", "the", "lazy", "dog"};
        Array.Resize(ref myArr, myArr.Length + 5);

        MyTest[] resultsMyTest = new MyTest[] {new MyTest{RoomCount=3,Cover="Red"}};
        //here  it will work  as the compiler know  how to infer it 
        Array.Resize(ref resultsMyTest, results.Length + 5);            


    }
    public class  MyTest
    {
        public int RoomCount
        {
            get;
            set; 
        }
        public string Cover
        {
            get;
            set;  
        }

    }


}

希望这个帮助

答案 1 :(得分:0)

尝试在您的Array.Resize<T>中添加显式类型:

Array.Resize<String>(ref results, lres);

然后将lres更改为0,因为lres是数组的长度,CopyTo的第二个参数是起始索引 目的地不是数组的长度:

res.CopyTo(results, 0);

索引是起始索引,你从最后一个索引开始,这就是目标数组不够长的原因。

或使用Array.Copy()

Array.Copy(res, results, res.length);

答案 2 :(得分:0)

如果您正在寻找当前代码的解决方案,请使用此方法,

var arguments=new object[] { results, results.Length + res.Length };
        var method= typeof(Array).GetMethod("Resize").MakeGenericMethod(res.GetType().GetElementType());
        method.Invoke(null,arguments );
        var lastIndex = results.Length;
        results = arguments[0] as Array;
        res.CopyTo(results, lastIndex);

但我强烈建议你不要这样做。但是创建一个包含所有这些属性的class并使用List<YourClass>来获得干净且可维护的代码。在您被迫进行完整的重新分解之前,匿名类的通用Array不会花费很长时间。