Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linq: retrieve list of weekdays along with linq query

Posted on 2011-02-19
4
Medium Priority
?
501 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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