Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I need use a query that'll get multiple rows and insert it in an Array. Please refer to the example below.
Ex.
  [Day]        |   [Fruit]
01/02/2014     |  {A, B, C, D}
01/03/2014     |  {A, C, D}
01/04/2014     |  {A, B, D, E}
01/05/2014     |  {A, C}
01/06/2014     |  {A, B, C, D, E}
01/07/2014     |  {A}


Here's my code:

using (var conn = new SqlConnection("DataSource Here"))
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "SELECT DISTINCT [Fruit], [DateReceive] FROM                                                      [FruitsDB].[dbo].[dummy] WHERE [Location]=@loca  AND [DateReceive] BETWEEN '2014-01-02' AND '2014-01-07' ORDER BY [DateReceive]";
                    cmd.Parameters.AddWithValue("@loca", DropDownList1.SelectedValue.ToString());
                    conn.Open();


                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var tmp = reader["Fruit"];
                            if (tmp != DBNull.Value || tmp2 != DBNull.Value)
                            {
                                ListBox1.Items.Add(tmp.ToString());
                            }
                            IList<string> list1 = new List<string>();
                            foreach (var items in ListBox1.Items)
                            {
                                list1.Add(items.ToString());
                            }
                            lbList.Text = string.Join(",", list1);
                        }
                    }
                }
            }



I hope you can help me guys.

SOLVED


-Using Christian G.'s FOR XML query

HERE'S THE SOLUTION
Thank you Mr. Christian Graus for helping me!

replacement for cmd.CommandText above
SELECT DAY(o.DateReceive) AS DayReceive,
        STUFF
        (
            (SELECT ',' + convert(varchar(99), s.Fruit)
            FROM [FruitsDB].[dbo].[dummy] s
            WHERE DAY(s.DateReceive) = DAY(o.DateReceive)
            AND Location = 'Quezon'
            GROUP BY s.Fruit
            ORDER BY s.Fruit
            FOR XML PATH('')
            ), 1, 1, ''
        ) AS Fruit
  FROM [FruitsDB].[dbo].[dummy] o
  WHERE o.DateReceive IS NOT NULL
  GROUP BY Day(o.DateReceive)

RESULT:
[Day] |   [Fruit]
  2   |  Apple, Orange, Grapes, Lemon
  3   |  Apple, Figs, Lemon
  4   |  Apple, Figs, Grapes, Strawberry
  5   |  Grapes, Lychee, Strawberry
  6   |  Apple, Orange, Grapes
  7   |  Apple, Figs, Grapes, Lychee
Posted
Updated 18-Jan-14 21:45pm
v6
Comments
Karthik_Mahalingam 18-Jan-14 15:42pm    
not clear..
can u pls add more info...
Christian Graus 18-Jan-14 16:34pm    
IT's VERY clear
Dev Jonie 18-Jan-14 21:23pm    
Thanks its very helpful
Maciej Los 18-Jan-14 17:12pm    
Please, post sample data and expected output.
BillWoodruff 18-Jan-14 19:58pm    
Your source list example is transformed into a List with three inner Lists. What's the connection with "in each day, for a Week" ?

And, what "organizing principle" do you wish to use to produce (7 ?) groups ? What if day one is "figs, bananas, oranges;" and, day two is "lemons, apples, watermelon:" how will you discriminate where one day ends, and another begins, if those two days are "in sequence" ?

Missing details on how date is used to parse the data ?

Hopefully, the very talented Christian G. will help put you on the track of an SQL solution that rocks, if that's what you seek.

It's easy to write procedural code in C# to take one key-term (name of a fruit), and parse the list of strings into a List containing inner Lists which would match the example transformation you show. Do you want to see an example of that ?

If you know your list of fruits, you an use pivot to get numbers of each. You can also use FOR XML to create a list of fruits for each day.

Here[^] is my article on how to do this.

If you post SQL to create your table structure and put data in to it, I'd be happy to write the SQL for you.

You've done this":

SQL
SELECT distinct DAY(DateReceive) AS DayReceive,
        STUFF
        (
            (SELECT ',' + convert(varchar(99), s.Fruit)
            FROM [FruitsDB].[dbo].[dummy] s
            WHERE DAY(s.DateReceive) = DAY(s.DateReceive)
            GROUP BY s.Fruit
            ORDER BY s.Fruit
            FOR XML PATH('')
             ), 1, 1, ''
        ) AS Fruit
  FROM [FruitsDB].[dbo].[dummy]
  WHERE Location = 'Quezon'



Note your where statement. Change it to WHERE DAY(s.DateReceive) = DAY(DateReceive) or better yet, give your outer table another alias and use that.

I also think you need to lose the DISTINCT, and instead use a group by. You will need to do groupby day(datereceive), I think.
 
Share this answer
 
v2
"[Now Proceeding in Next Phase]"

The QA format gets broken if one question is actually a ton of questions. Ask this as a new question and I'll be happy to answer it.

Looks like you're not about, and I am going out. I'd store those values in a dictionary, but the other option is a class like this:

class MyObject
(
public int id;
public List<string> Fruits;
)

and then a List<MyObject>

Obviously, a dictionary would map an id to a list of strings, also.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900