Solved

Linq: retrieve list of weekdays along with linq query

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now