如何从sql查询结果生成数组?

wkftcu5l  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(421)

我不想从1数组和sql mysqldatareader sdr.read()结果中得到数组结果[1000,0,0,500,0,0,0,3210,0]。
这是数组

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];
jdgnovmf

jdgnovmf1#

短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+" ");
            });
        }
    }

相关问题