Solved

Linq: retrieve list of weekdays along with linq query

Posted on 2011-02-19
4
498 Views
Last Modified: 2012-05-11
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
Comment
Question by:Robert Treadwell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 34936850
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
 

Author Comment

by:Robert Treadwell
ID: 34944521
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
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 34950520
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
 
LVL 27

Expert Comment

by:MikeToole
ID: 35007175
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question