Click here to Skip to main content
14,932,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a db table and I want to use Linq to map the db fields to a list of objects. Here is my Class definition.

VB
Public Class CalendarMonth
        Public Property Run_Name As String
        Public Property Month_Scope As List(Of MonthScope)
End Class

and my MonthScope class looks like this:
VB
Public Class MonthScope
        Public Property h_yyyy As Integer?
        Public Property h_mm As Integer?
        Public Property h_mon As String
End Class

I want to Query the below table so that I get the below:
2019-1
      2019 1 Jan
      2019 2 Feb
      2019 3 Mar
      2019 4 Apr
2019-2
      2019 5 May
      2019 6 Jun
      2019 7 Jul
      2019 8 Aug
2019-3
      2019 9 Sep
      2019 10 Oct
      2019 11 Nov
      2019 12 Dec

Below is my data set
RUN_NAME H_YYYY	H_MM	H_MON
2019-1	2019	1	Jan
2019-1	2019	2	Feb
2019-1	2019	3	Mar
2019-1	2019	4	Apr
2019-2	2019	5	May
2019-2	2019	6	Jun
2019-2	2019	7	Jul
2019-2	2019	8	Aug
2019-3	2019	9	Sep
2019-3	2019	10	Oct
2019-3	2019	11	Nov
2019-3	2019	12	Dec


What I have tried:

C#
CalendarMonthList = (From rw As DataRow In dt.Rows Select New CalendarMonth With {
.Run_Name = rw("run_name").ToString,                                                                                  .Month_Scope = (From rw1 As DataRow In dt.Rows Where rw1("run_name") = .Run_Name Select New MonthScope With {.h_yyyy = rw1("h_yyyy"),                                                                                                                                                                                            .h_mm = rw1("h_mm"),                                                                                                                                                                                               .h_mon = rw1("h_mon")}).Distinct.ToList()}).Distinct.ToList()
Posted
Updated 25-Dec-20 0:29am
v3
Comments
BillWoodruff 24-Dec-20 21:49pm
   
Do you have control over the format of table ?
Ridge Howison 26-Dec-20 0:53am
   
Not really. That's how I get the data. I'm trying to map to the classes I have. There maybe a group by option, but I'm not sure how to do it.
BillWoodruff 26-Dec-20 1:03am
   
"There maybe a group by option, but I'm not sure how to do it." What's stopping you from studying 'groupby, and experimenting with it ?
Ridge Howison 27-Dec-20 0:55am
   
Well, that’s what I’ve been doing without success.
BillWoodruff 29-Dec-20 21:06pm
   
If you show the actual structure for the DataSet (or, for each DataSet in the DataTable), I think you may get more specific responses,
Ridge Howison 2-Jan-21 19:11pm
   
Sure. This is the dataset from my database. There are 4 columns
a. RUN_NAME
b. H-YYYY
c. H_MM
d. H_MON

RUN_NAME H_YYYY H_MM H_MON
2019-1 2019 1 Jan
2019-1 2019 2 Feb
2019-1 2019 3 Mar
2019-1 2019 4 Apr
2019-2 2019 5 May
2019-2 2019 6 Jun
2019-2 2019 7 Jul
2019-2 2019 8 Aug
2019-3 2019 9 Sep
2019-3 2019 10 Oct
2019-3 2019 11 Nov
2019-3 2019 12 Dec
BillWoodruff 2-Jan-21 21:38pm
   
until i have a better crystal ball :) ... what's critical is knowing the structure of the Columns ... like this:

dt.Columns.Add(new DataColumn("TypID")
{
AllowDBNull = false,
DataType = typeof(System.Char),
Unique = false
});

1 solution

   
Comments
BillWoodruff 27-Dec-20 0:50am
   
+5

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