繁体   English   中英

如何从 sql 查询结果创建一个数组?

[英]How can I make an array from a sql query result?

我不想从 1 个数组和 sql MySqlDataReader sdr.Read() 结果中得到数组结果 [1000, 0, 0, 500, 0, 0, 0, 0, 3210, 0]。

这是 arrays

ArrayList array1 = new ArrayList()[1,2,3,4,5,6,7,8,9,10];
ArrayList arrayresult = new ArrayList();
while (sdr.Read())
{
      transactions.Add(new transaction_details
            {
                  ID = Int32.Parse(sdr["ID"].ToString()),
                  Transdate = DateTime.Parse(sdr["Transdate"].ToString()),
                  Debit = Decimal.Parse(sdr["Debit"].ToString()),
                  TransactionName = sdr["TransactionName"].ToString()
            });
}
//arrayresult.add() - what do I do with this ?

如果 sdr["ID"].ToString() 中的 ID 在 array1 中,则结果将为 [ 1, 4, 9]。 array1 有 10 个索引,所以对于最终结果,我希望它是 [1, 0, 0, 4, 0, 0, 0, 0, 9, 0]。 如果 ID 不存在,将插入 0。

我需要 arrayresult 才能得到这个结果。

var debit = [1000, 0, 0 ,500, 0, 0, 0, 0, 3210, 0];
var ID = [ 1, 0, 0, 4, 0, 0, 0, 0, 9, 0];
var date= [ "2020-01-01", 0, 0, "2020-01-02", 0, 0, 0, 0, "2020-01-03", 0];

短 linq 伪代码:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    public class Program
    {
        public static void Main()
        {
            List<int> inp = new List<int>(){1,4,9};

            List<int> array1 = new List<int>(){1,2,3,4,5,6,7,8,9,10};
            var array1Index = array1.Select((i, index) => new { Index=index, i=i }).ToList();

            List<int> arrayresult = new List<int>();

            var query = from p in array1Index
                select inp.Contains(p.i)==true?p.i:0;

            arrayresult = query.ToList();
            arrayresult.ForEach(x=>{
                Console.Write(x+" ");
            });
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM