How to orderby a linq query with a join to two tables and output the data to a predefined datatable

I need to order this linq query by ID and then by Date
I have two datatables dt1 and dt2

and then I have a predefined table called joinedTable with columns ID, Code, Date, Price

DataTable joinedTable = GetDataTable();
 var result = from dataRows1 in infocodesDT.AsEnumerable()
                         join dataRows2 in mqDT.AsEnumerable()
                         on dataRows1["InfoCode"] equals dataRows2["InfoCode"]

                         select joinedTable.NewRow().ItemArray = new object[]{
                             dataRows1["SecurityID"],
                             dataRows2["MarketDate"],
                             dataRows2["OpenPrice"],
                             dataRows2["ClosePrice"],                        
                             dataRows2["VWAP"],
                             dataRows2["TotalReturnIndex"],
                         };
LVL 1
countrymeisterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi countrymeister;

I have reformatted the query to supply the data types of the field for the query Of course they are most likely incorrect so you'll need to check them. The ordering of the results is placing the first field to be ordered by first then by the secon field. In the select clause it takes a Type and NOT a statement so if you need to fill a datatable create a class as I did below and use that in the select clause then you can iterate through the result set and fill each row of the table.

var result = from dataRows1 in infocodesDT.AsEnumerable()
             join dataRows2 in mqDT.AsEnumerable()
             on dataRows1.Field<int>("InfoCode") equals dataRows2.Field<int>("InfoCode")
             orderby dataRows1.Field<int>("InfoCode"), dataRows2.Field<DateTime>("MarketDate")
             select new JoinedResults {
                 SecurityID = dataRows1.Field<int>("SecurityID"),
                 MarketDate = dataRows2.Field<DateTime>("MarketDate"),
                 OpenPrice = dataRows2.Field<decimal>("OpenPrice"), 
                 ClosePrice = dataRows2.Field<decimal>("ClosePrice"),
                 VWAP = dataRows2.Field<int>("VWAP"),
                 TotalReturnIndex = dataRows2.Field<int>("TotalReturnIndex")
             };
             
public class JoinedResults
{
    public int SecurityID { get; set; }
    public DateTime MarketDate { get; set; }
    public decimal OpenPrice { get; set; }
    public decimal ClosePrice { get; set; }
    public int VWAP { get; set; }
    public int TotalReturnIndex { get; set; }    
}

Open in new window

0
countrymeisterAuthor Commented:
FernandoSoto

What do you think about this

var result = (from dataRows1 in infocodesDT.AsEnumerable()
                          join dataRows2 in mqDT.AsEnumerable()
                          on dataRows1["InfoCode"] equals dataRows2["InfoCode"]

                          select joinedTable.NewRow().ItemArray = new object[]{
                             dataRows1["SecurityID"],
                             dataRows2["MarketDate"],
                             dataRows2["OpenPrice"],
                             dataRows2["ClosePrice"],                        
                             dataRows2["VWAP"],
                             dataRows2["TotalReturnIndex"],
                         }).OrderBy(m => m.ElementAt<object>(0)).ThenBy(m => m.ElementAt<object>(1));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Does this,   select joinedTable.NewRow().ItemArray = new object[]{,  actually work on your system?
0
countrymeisterAuthor Commented:
Yes, that works, and so does the last linq query I posted with the order by.
I do not have to create that extra class. Give it a try and let me know.
0
countrymeisterAuthor Commented:
I did not have to create a new class
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.