如何在Array中获取数组。 [续] [英] How do I get an arrays inside an Array. [contd]
问题描述
我有一个查询,我想将结果作为数组插入并将其插入单个数组中
我试图在每一天选择不同的行,一周。
ex。
I have a query and I want to insert results as an Arrays and insert it in a single Array
I'm trying to get selected Distinct Rows in each day, for a Week.
ex.
{{A,B,C,D},{A,C,D},{A,B}}
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
myItems = new Itemset(); //this is where I store items in 1 array
myDb = new ItemsetCollection();// this is where I want to store multiple
//arrays
var tmp = reader["Fruit"];
if (tmp != DBNull.Value)
{
ListBox1.Items.Add(tmp.ToString());
}
foreach (var items in ListBox1.Items)
{
myItems.Add(items.ToString());
}
foreach (var items in myItems)
{
myDb.Add(new Itemset() {items.ToString()});
}
lbList.Text = string.Join(",\n", myItems.ToArray());
}
}
我的代码的结果如下:
苹果,橘子,葡萄,柠檬,苹果,葡萄,苹果,橙子,柠檬。
它远远超出了我的想象。
{{Apple,Orange,Grapes,Lemon},{Apple,Grapes},{Apple,Orange,Lemon}}
我需要一个C#代码在一个数组中插入一组数组。
我试过foreach将它存储在数组中。
我需要的最终结果
The result from my code is this:
Apple, Orange, Grapes, Lemon, Apple, Grapes, Apple, Orange, Lemon.
Its so far from what I want it to become.
{{Apple, Orange, Grapes, Lemon}, {Apple, Grapes}, {Apple,Orange, Lemon}}
I need a C# code to insert a set of arrays in a single Array.
I tried foreach to store it in array.
THE FINAL RESULT I NEED
[Week] | [AllFruits]
1 | {Apple, Orange, Grapes, Lemon},{Apple, Figs, Lemon}, {Apple, Figs, Grapes, Strawberry},{Grapes, Lychee, Strawberry},{Apple,Orange,Grapes},{Apple,Figs,Grapes,Lychee}
我希望你明白我想要处理什么。
我研究了大部分需要的东西信息只是为了完成这个过程,但我仍然缺少/错过了一些重要的部分。
I hope you understand what I want to process.
I researched most of the needed information just to do this process but still I lack/miss some important part.
推荐答案
所以,有两种方法可以做到这一点。
首先,创建一个这样的字典
字典< int,List< string> >
这将映射您可以按序数访问或通过Keys集合访问的星期列表到字符串列表。
其次,创建一个包含您的周数和字符串列表的类。然后创建一个列表。
字典的好处是你不能在同一周添加两次,你可以轻松访问任何一周,或者所有通过一个for ....每个循环。
So, there's two ways to do this.
First, create a Dictionary like this
Dictionary <int, List<string> >
This will map your list of weeks, which you can access by ordinal or iterate over through the Keys collection, to lists of strings.
Second, create a class that contains your week number, and a list of strings. Then create a List of those.
The benefits of the dictionary are that you can not add the same week twice, and you can access any week easily, or all the weeks through a for....each loop.
这篇关于如何在Array中获取数组。 [续]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!