• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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));
0
countrymeister
Asked:
countrymeister
  • 3
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
Fernando SotoCommented:
Did that work out for you?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Fernando SotoCommented:
Do you need anymore assistance with this?
0
 
Sameer RaksheCommented:
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now