Solved

linq query with ordered columns

Posted on 2011-09-30
4
314 Views
Last Modified: 2013-11-11
I'm new to Linq and I'd like to rewrite the query I use to return results from a DomainService so that the columns are returned and a specific order.  If this was SQL I would use somthing like SELECT col1, col2, col3 from myTable.  The rows will be loaded into a datagrid in a Silverlight app.
public IQueryable<myTable> GetMyTable()
        {
            return this.ObjectContext.myTable.OrderBy(e => e.ItemID);
            
        }

Open in new window

0
Comment
Question by:PeterTrefren
[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
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36893417
I'm not 100% on this, but have you tried switching the order of the properties as defined in your myTable class?
0
 
LVL 14

Expert Comment

by:binaryevo
ID: 36893490
Im not sure how you would do it with a datatable but, the syntax looks like this if you have a specific object your using:

var test = from t in myListFromDomainObject
                 select new BindingObjectThatICreated
                 {
                      ObjectVal = t.ObjectVal,
                      ObjectVal2 = t.ObjectVal2
                 }

Here are some pretty good basic examples:  http://msdn.microsoft.com/en-us/vcsharp/aa336756

Hope this helps, if you need more direction just let me know.

0
 

Accepted Solution

by:
PeterTrefren earned 0 total points
ID: 36920647
binaryevo: I tried your solution and was getting an error on return from the DomainService to get the data.  The following thread post http://forums.silverlight.net/p/119935/270919.aspx has what you're talking about as a solution but the person who posted points out a limitation of this kind of solution.  


For what I want to do, displaying columns in a specific order in a grid, the code I've attached works the best.




<riaControls:DomainDataSource Name="questionDataSource" LoadSize="20" QueryName="GetTestQuestions" AutoLoad="True">
                <riaControls:DomainDataSource.DomainContext>
                    <ds:QuestionContext></ds:QuestionContext>
                </riaControls:DomainDataSource.DomainContext>
            </riaControls:DomainDataSource>

            <sdk:DataGrid AutoGenerateColumns="True"  Name="dataGrid1" Width="Auto" Height="Auto" MinHeight="100" 
                          ItemsSource="{Binding Data, ElementName=questionDataSource}" VerticalScrollBarVisibility="Visible">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="QuestionID" Binding="{Binding QuestionID}"  />

                    <!--Question-->
                    <sdk:DataGridTemplateColumn  Header="Question" >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Question}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

                    <!--Ans1-->
                    <sdk:DataGridTemplateColumn  Header="Ans1" >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Ans1}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

                    <!--Ans2-->
                    <sdk:DataGridTemplateColumn  Header="Ans2" >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Ans2}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

                    <!--Ans3-->
                    <sdk:DataGridTemplateColumn  Header="Ans3" >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Ans3}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

                    <!--Ans4-->
                    <sdk:DataGridTemplateColumn  Header="Ans4" >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Ans4}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>


                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

Open in new window

0
 

Author Closing Comment

by:PeterTrefren
ID: 36941267
This provides the what I wanted.
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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