Solved

linq query with ordered columns

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

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

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

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…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

19 Experts available now in Live!

Get 1:1 Help Now