I do have a dataset with

id date code
1 02/02/2012 A
1 02/07/2012 B
1 02/21/2012 A
2 02/02/2012 C
2 02/09/2012 A

and I have to create a grid with the days in february and insert the code in the coresponding days

id 1 2 3 4 5 6 7 8 9 10 .............................

1 A B A
2 C A

Can I do this with Linq or I have to do it wiht coding

Edited by kukuruku

1 Year
Discussion Span
Last Post by tinstaafl

I cannot think of LINQ query to do this. I think the easiest way to do this would be to use a Dictionary, something along the lines of the following :

Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();



Possibly you might be able to do.

I know you can use this piece of code to convert a column from a DataTable into an array (value is the DataTable, and ColumnName is the column's name)
return value.Rows.Cast<DataRow>().Select(row => row [ColumnName]).Cast<T>().ToArray();

You might be able to do something like this, however, getting that grouping you desire is another issue. I'll have to think on this one a little longer.


While you might be able to get a LINQ query to work, I would suggest using code or a combination of both. Since you seem to want to pivot the table twice, the LINQ query will most likely be quite complicated and will severely impact the maintainability of your code without any real advantage of speed.

Here's one example of how such a code might look like:

public static DataTable MakeTable(DataSet ds, int month, int year)

    DataTable dt = new DataTable();
    for (int i = 1; i <= 10; i++)
    int days = DateTime.DaysInMonth(year, month);
    for (int i = 1; i <= days; i++)
        DataRow tempRow = dt.NewRow();
        tempRow.SetField<int>(0, i);
    foreach (DataRow dr in ds.Tables[0].Rows)
        DateTime dataDate = Convert.ToDateTime(dr.Field<string>(1));
        if (dataDate.Month == month)
            dt.Rows.Find(dataDate.Day).SetField<string>(dr.Field<string>(0), dr.Field<string>(2));
    return dt;

dataGridView1.DataSource = MakeTable(myDataSet, 2, 2015);

Edited by tinstaafl

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.