How to "Linq to DataTable" using Own DataTable Class

Viewed 41

How can I put whatever is coming out of this linq query to my own DataTable Class?

public class MyDataTable : DataTable
{
    public MyDataTable()
    {
        this.Columns.Add("ColumnName1");
        this.Columns.Add("ColumnName2");
        this.Columns.Add("ColumnName3");
        this.Columns.Add("ColumnName4");
    }
}

My linq query:

MasterDataTable
.AsEnumerable()
.Where(rr =>
            rr.Field<string>("ColumnName1") == SelectedValue1 &&
            rr.Field<string>("ColumnName2") == SelectedValue2 &&
            rr.Field<string>("ColumnName3") == SelectedValue3 &&
            rr.Field<string>("ColumnName4") == SelectedValue4)
).Select(rr => rr);

I have tried this but it I am warned that I cannot convert an EnumerableRowCollection to MyDataTable

MyDataTable dt = MasterDataTable
    .AsEnumerable()
    .Where(rr =>
                rr.Field<string>("ColumnName1") == SelectedValue1 &&
                rr.Field<string>("ColumnName2") == SelectedValue2 &&
                rr.Field<string>("ColumnName3") == SelectedValue3 &&
                rr.Field<string>("ColumnName4") == SelectedValue4)
    ).Select(rr => rr);

So then I tried to get it all in an EnumerableRowCollection but here I am getting "Embeded statement cannot be a declaration or labeled statement"

EnumerableRowCollection<DataRow> selection = ConfigurationDataTable
.AsEnumerable()
.Where(rr =>
                rr.Field<string>("ColumnName1") == SelectedValue1 &&
                rr.Field<string>("ColumnName2") == SelectedValue2 &&
                rr.Field<string>("ColumnName3") == SelectedValue3 &&
                rr.Field<string>("ColumnName4") == SelectedValue4)
).Select(rr => rr);

What am I doing wrong?

2 Answers

I threw the custom class out and just used the native DataTable class for both datatables. This seems to do the trick.

DataTable dt = MasterDataTable
    .AsEnumerable()
    .Where(rr =>
                rr.Field<string>("ColumnName1") == SelectedValue1 &&
                rr.Field<string>("ColumnName2") == SelectedValue2 &&
                rr.Field<string>("ColumnName3") == SelectedValue3 &&
                rr.Field<string>("ColumnName4") == SelectedValue4)
    ).Select(rr => rr)
    .CopyToDataTable();

A DataTable does not inherit from IEnumerable:

https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.select

You would probably be best to create a custom class that inherits from IEnumerable and create methods to easily insert your data using a DataTable.

Here is how you would manually read the data:

// Create your DataTable
var t = new DataTable();
t.Columns.Add("ColumnName1");   // Add your column names
t.Columns.Add("ColumnName2");   // (it is better to use one of the overload
t.Columns.Add("ColumnName3");   //  methods that defines the row's data type
// Create an instance of your new DataRow:
var row = t.NewRow();
// populate your data into your new row:
row["ColumnName1"] = "Bill";
row["ColumnName2"] = "Ted";
row["ColumnName3"] = "Excellent";
// add the DataRow to your DataTable
t.Rows.Add(row);
// Now retrieve the data using something that inherits from IEnumerable
var list = new List<String>();
// Loop through your DataTable to get the individual records
foreach (DataRow row1 in t.Rows) {
    // notice that above you MUST cast the row. It fails if you use "var row".
    list.Add(String.Format("{0} {1} {2}", row["ColumnName1"], row["ColumnName2"], row["ColumnName3"]));
}
// return your IEnumerable item
return list;

I could give you better examples of how to use the DataRow to create an IEnumerable class object, but that is a lot of code. If you need it, let me know.

Related