Solved

Linq: retrieve list of weekdays along with linq query

Posted on 2011-02-19
4
494 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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