Solved

linq query with ordered columns

Posted on 2011-09-30
4
315 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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