LINQ to return DataRows

I hava linq query which returns a var object, I would like to cast it to return DataRows , as in DataTable.Rows

 var listPrices = (from dataRows1 in dtPrices.AsEnumerable()
              join dataRows2 in dtMultiplier.AsEnumerable()
              on dataRows1["ID"].ToString() equals dataRows2["ID"].ToString()
              select dtPrices.NewRow().ItemArray = new object[]{
                   dataRows1["ID"],
                   dataRows1["BusDate"],
                   dataRows1["EndPrice"] = dataRows1["EndPrice"] == DBNull.Value ? 0 : (dataRows2["Multiplier"] == DBNull.Value ? Convert.ToDecimal(dataRows1["EndPrice"]) : Convert.ToDecimal(dataRows1["EndPrice"]) / Convert.ToDecimal(dataRows2["Multiplier"])),
                   dataRows1["StartPrice"] = dataRows1["StartPrice"] == DBNull.Value ? 0 : (dataRows2["Multiplier"] == DBNull.Value ? Convert.ToDecimal(dataRows1["StartPrice"]) : Convert.ToDecimal(dataRows1["StartPrice"]) / Convert.ToDecimal(dataRows2["Multiplier"])),
                  
              }).OrderBy(m => m.ElementAt<object>(0)).ThenBy(m => m.ElementAt<object>(1));
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;

There is no direct way to do this. you will need to create your own way such as creating a DataTable and columns and iterating through the collection and filling the rows or you can implement the code from the following link to do it for you automatically.

How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRow
0
countrymeisterAuthor Commented:
I did it by iterating through the var object

 DataTable dt = GetDataTable();
                    DataRow newRow;
                    foreach (var item in listPrices)
                    {
                        newRow = dtPrices.NewRow();
                        newRow.ItemArray = item;
                        dt.Rows.Add(newRow.ItemArray);
                    }
                    joinedTable = dt;
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:
Did that work out for you?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Fernando SotoRetiredCommented:
Do you need anymore assistance with this?
0
Sameer Rakshesoftware developerCommented:
Please go through below code for solution:
	static DataTable ConvertListToDataTable(List<string[]> list)
	{
	    // New table.
	    DataTable table = new DataTable();

	    // Get max columns.
	    int columns = 0;
	    foreach (var array in list)
	    {
		if (array.Length > columns)
		{
		    columns = array.Length;
		}
	    }

	    // Add columns.
	    for (int i = 0; i < columns; i++)
	    {
		table.Columns.Add();
	    }

	    // Add rows.
	    foreach (var array in list)
	    {
		table.Rows.Add(array);
	    }

	    return table;
	}
    

Open in new window

0
countrymeisterAuthor Commented:
I solved my issue
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.