[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

Linq: retrieve list of weekdays along with linq query

I have a gridview which display organization data.  One of the columns should contain a list of 'days of operation'.  I capture these days using a checklist and store in sql table by an 'OrgID'.  I want want to retrieve those days and display in column.  Currently I am retrieving all other column values using a Linq query.  The selection of the 'Days of Operation' creates multiple listings in my Gridview.  I want the list of Days of Operation to be listed in one column.  How do I do this?
0
Robert Treadwell
Asked:
Robert Treadwell
  • 3
1 Solution
 
MikeTooleCommented:
More information is needed.
What columns are in the table?
Are there multiple days of operation for one organisation?
Can you post the LINQ query that produces the wrong result?
0
 
Robert TreadwellAuthor Commented:
What columns are in the table?
See GridView code below for column details.

Are there multiple days of operation for one organisation?
Yes, I'm using a checkboxlist, allowing user to select the number of days.

Can you post the LINQ query that produces the wrong result?
See code behind below.  This selection produces multiple records in gridview when I only want one record with a list of the days of operation.
.aspx
 <asp:GridView ID="Client" runat="server" AllowPaging="True" GridLines="None"
            AllowSorting="True" AutoGenerateColumns="False" width="100%" AlternatingRowStyle-BackColor="#999999" 
            AlternatingRowStyle-ForeColor="#e1e1e1" AlternatingRowStyle-Font-Size="Smaller" HeaderStyle-Font-Bold="true" 
            HeaderStyle-Font-Size="Small" HeaderStyle-BackColor="#333333" HeaderStyle-ForeColor="White"
            RowStyle-Font-Size="Smaller" RowStyle-BackColor="#ffffff" HorizontalAlign="Left">
            <Columns>
                <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="cbSelectAll" runat="server" Text="" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox id="Selector" runat="server"  />
                 </ItemTemplate>
                </asp:TemplateField>
                <asp:HyperLinkField DataTextField="OrgName" HeaderText="Organization Name" DataNavigateUrlFields="RecordID"
                 DataNavigateUrlFormatString="~/Forms/OKE.aspx?id={0}" SortExpression="OrgName" ControlStyle-ForeColor="#000000" /> 
                <asp:BoundField DataField="DirectorName" HeaderText="Director's Name"/>
                <asp:BoundField DataField="DayOfOperation" HeaderText="Day of Operation"/>
                <asp:BoundField DataField="ShippingFullAddress" HeaderText="Shipping Address"/>
                <asp:BoundField DataField="OrgDescription" HeaderText="Organization Description"/>
            </Columns>

        </asp:GridView>

.code behind
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim db As OrgInfoDataContext = New OrgInfoDataContext
        Dim query = From obi In db.OrgBasicInfos
                    Join oai In db.OrgAddressInfos On obi.RecordID Equals oai.OrgID _
                     Join dow In db.OrgDayOfWeeks On obi.RecordID Equals dow.OrgID _
                     Join occ In db.OrgContacts On obi.RecordID Equals occ.OrgID
            Select obi.RecordID, _
            obi.OrgName, _
            obi.OrgDescription, _
            oai.ShippingFullAddress, _
            occ.DirectorName, _
            DayOfOperation = dow.WeekDay

        Client.DataSource = query
        Client.DataBind()
    End Sub

Open in new window

0
 
MikeTooleCommented:
The Join on dow produces the unwanted result.
What is needed is for the days to be grouped before being added to the output.
There are probably several ways to achieve this, the attached example uses a sub-query to do it.

Query R1 produces the result you don't want - multiple records per customer, one for each matching day.
R2 gets an array of the days for each customer and uses the String.Join() method to concatenate them into a comma separated list.

Hope this helps  
 
Imports System.Linq
Module LinqTest

    Public Sub GroupDays()
        LoadData()
        Dim R1 = From C In Customers
                     Join D In CustDays On C.CustomerID Equals D.CustomerID
                     Select C.CustomerID, D.DOW
        For Each obj In R1
            Console.WriteLine(String.Format("Customer: {0}, Day: {1}", obj.CustomerID, obj.DOW))
        Next
        Console.ReadLine()
        Dim R2 = From C In Customers
                 Select C.CustomerID, DL = String.Join(",", (From D In CustDays Where C.CustomerID = D.CustomerID Select D.DOW).ToArray)
        For Each obj In R2
            Console.WriteLine(String.Format("Customer: {0}, Day: {1}", obj.CustomerID, obj.DL))
        Next
        Console.ReadLine()
    End Sub

    Dim Customers As New List(Of Customer)
    Dim Orders As New List(Of Order)
    Dim CustDays As New List(Of CustDay)
    Private Sub LoadData()
        Customers.Add(New Customer With {.CustomerID = 1, .Name = "AAA"})
        Customers.Add(New Customer With {.CustomerID = 2, .Name = "BBB"})
        Customers.Add(New Customer With {.CustomerID = 3, .Name = "CCC"})
        CustDays.Add(New CustDay With {.CustomerID = 1, .DOW = "Wed"})
        CustDays.Add(New CustDay With {.CustomerID = 2, .DOW = "Mon"})
        CustDays.Add(New CustDay With {.CustomerID = 2, .DOW = "Fri"})
        CustDays.Add(New CustDay With {.CustomerID = 3, .DOW = "Thu"})
        CustDays.Add(New CustDay With {.CustomerID = 3, .DOW = "Wed"})
        CustDays.Add(New CustDay With {.CustomerID = 3, .DOW = "Tue"})

        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 23})
        Orders.Add(New Order With {.CustomerID = 1, .OrderID = 11})
        Orders.Add(New Order With {.CustomerID = 1, .OrderID = 13})
        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 27})
        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 24})
        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 26})
        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 25})
        Orders.Add(New Order With {.CustomerID = 3, .OrderID = 39})
        Orders.Add(New Order With {.CustomerID = 1, .OrderID = 12})
        Orders.Add(New Order With {.CustomerID = 3, .OrderID = 31})
        Orders.Add(New Order With {.CustomerID = 3, .OrderID = 32})
        Orders.Add(New Order With {.CustomerID = 2, .OrderID = 22})
    End Sub
    Class Customer
        Property CustomerID As Integer
        Property Name As String
    End Class
    Class Order
        Property CustomerID As Integer
        Property OrderID As Integer?
    End Class
    Class CustDay
        Property CustomerID As Integer
        Property DOW As String
    End Class
End Module

Open in new window

0
 
MikeTooleCommented:
These are the results produced by the code I posted, does the second query match what you want?
R1: This corresponds to your current output
Customer: 1, Day: Wed
Customer: 2, Day: Mon
Customer: 2, Day: Fri
Customer: 3, Day: Thu
Customer: 3, Day: Wed
Customer: 3, Day: Tue

R2: Here the "days" for each customer are grouped into a comma separated string
Customer: 1, Day: Wed
Customer: 2, Day: Mon,Fri
Customer: 3, Day: Thu,Wed,Tue
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now