Solved

linq query with ordered columns

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

11 Experts available now in Live!

Get 1:1 Help Now