AsDataView Does Not Show Up - c#

I would like to return the linq result as a dataview, But AsDataView does not show up as a property in intellisense. I do have System.Data.DataSetExtensions as a reference
var qry = from s in MedianDB.tblCountries.AsEnumerable() select s;
DataView dv = qry.AsDataView(); //error here

AsDataView is a DataTable extension method.
you should work on the DataTable directly:
DataView dv = MedianDB.tblCountries.AsDataView();

Related

Convert linq query result to dataview

I'm querying the database and returning a row.
var p = (from prop in db.tabe
where prop.pid == 1
select prop);
However I'd like to take the result and insert that into a dataview. Is there a way?
"Creating a DataView from a query that returns anonymous types or queries that perform join operations is not supported."
So that is not supported, why don't you use List instead:
var list = (from prop in db.tabe
where prop.pid == 1
select prop).ToList();
You can do this:
DataView view = p.AsDataView();
bindingSource1.DataSource = view;
If you just want the result as a dataview you should be able to do this:
Dataview pDV = p.AsDataView();
This should be a full detail example and additional assistance:
http://msdn.microsoft.com/en-us/library/bb669073(v=vs.110).aspx
You could try:
DataView v = new p.AsDataView();
or
DataTable t = new DataTable();
foreach (DataRow dataRow in p)
t.Rows.Add(dataRow);
DataView v = new DataView(t);

DataSet sorting

In DataTable I could sorting with
dataTable.DefaultView.Sort = "SortField DESC";
I'm getting a DataSet from database, I was wondering could I do a sorting on the DataSet like how I do it in DataTable.
you can still access the DataTable from the the data set as follows,
ds.Tables[0].DefaultView.Sort =" criterian";
Hope this helps.
DataView view = ds.Tables[0].DefaultView;
view.Sort = "SortField DESC";
http://msdn.microsoft.com/en-us/library/1ay5y4w0(v=vs.71).aspx
http://social.msdn.microsoft.com/Forums/nl/netfxbcl/thread/adbd95cd-49d1-483d-b2b2-4b696a66e9a6
Access the DataTable from the the DataSet as follows,
ds.Tables[0].DefaultView.Sort = "SortField DESC";
Hope this helps.
For advanced sorting needs you might want to use LINQ like described here. Basically it allows creating a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiew extension method.
Alternatively if you are OK with (or maybe even prefer) using an IEnumerable
you could use the System.Data.DataTableExtensions.AsEnumerable extension method. For example:
var enumerable = dataSet.Tables[0].AsEnumerable()
.OrderBy(x => x.Field<string>("ColumnName")
.ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);
From tha DataSet object, you can access all the DataTable to intract with.
Try this:
DataDet.Tables[0].DefaultView.Sort = "sort criteria";
DataSet fileTransferDetail = null;//Data to be sorted.
DataSet result = null;//Declare a dataSet to be filled.
//Sort data.
fileTransferDetail.Tables[0].DefaultView.Sort = "ID DESC";
//Store in new Dataset
result.Tables.Add(fileTransferDetail.Tables[0].DefaultView.ToTable());
Try the following code.
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort=value;

How to pass DataTable.Select() result to a new DataTable?

I have a DataTable named dt2 with data. I am calling its Select method to get some specific rows.
DataRow[] foundRows;
expression = "parent_id=1";
foundRows = dt2.Select(expression);
How can I pass the Select-method result to a new DataTable – say FilteredData?
You can use CopyToDataTable, available on IEnumerable<DataRow> types.
var filteredData = dt2.Select(expression).CopyToDataTable();
Just for clarity, the Select method returns an array of type DataRow. That's why we need to use CopyToDataTable(). Alex's answer is good. However, if the Select didn't return any rows, CopyToDataTable() will throw an InvalidOperationException.
So test that there is at least one DataRow before using the CopyToDataTable().
var filteredDataRows = dt2.Select(expression);
var filteredDataTable = new DataTable();
if(filteredDataRows.Length != 0)
filteredDataTable = filteredDataRows.CopyToDataTable();
Why not use a DataView instead?
DataView view = new DataView(dt2);
view.RowFilter = "parent_id = 1";
DataView will behave in very much the same way that a DataTable would with the added benefit that any change(s) to the underlying DataTable (dt2 in this case) would be automatically reflected in the DataView.

How do I limit the number of rows in a datatable?

I generate a DataTable (from non-SQL data) and then use a DataView to filter the records.
I want to limit the number of records in the final record set but can't do this when I generate the DataTable.
I've resorted to deleting rows from the final result set, as per:
DataView dataView = new DataView(dataTable);
dataView.RowFilter = String.Format("EventDate > '{0}'", DateTime.Now);
dataView.Sort = "EventDate";
dataTable = dataView.ToTable();
while (dataTable.Rows.Count > _rowLimit)
dataTable.Rows[dataTable.Rows.Count - 1].Delete();
return dataTable;
Is there a more efficient way to limit the results?
You can use Linq:
Try changing your code to the following:
DataView dataView = new DataView(dataTable);
dataView.RowFilter = String.Format("EventDate > '{0}'", DateTime.Now);
dataView.Sort = "EventDate";
dataTable = dataView.ToTable();
while (dataTable.Rows.Count > _rowLimit)
{
dataTable = dataTable.AsEnumerable().Skip(0).Take(50).CopyToDataTable();
}
return dataTable;
You'll need namespace: System.Linq and System.Data
Can you add a column which will be similar like rowcount 1,2,3
Something like incrementing as you add rows from your Non-SQL data to the datatable.
Probably after that you can use
DataRow[] rows1 = dTable.Select(" RowIncrement < 50", "EventDate ASC");
RowIncrement is the column which would be something like ROWNUM
Try copying the top x rows from one datatable into a new one.
dataTable.AsEnumerable().Take(50).CopyToDataTable(newdataTable)
and then use that.

LINQ query on a DataTable

I'm trying to perform a LINQ query on a DataTable object and bizarrely I am finding that performing such queries on DataTables is not straightforward. For example:
var results = from myRow in myDataTable
where results.Field("RowNo") == 1
select results;
This is not allowed. How do I get something like this working?
I'm amazed that LINQ queries are not allowed on DataTables!
You can't query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. You need to use the AsEnumerable() extension for DataTable. Like so:
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
And as #Keith says, you'll need to add a reference to System.Data.DataSetExtensions
AsEnumerable() returns IEnumerable<DataRow>. If you need to convert IEnumerable<DataRow> to a DataTable, use the CopyToDataTable() extension.
Below is query with Lambda Expression,
var result = myDataTable
.AsEnumerable()
.Where(myRow => myRow.Field<int>("RowNo") == 1);
var results = from DataRow myRow in myDataTable.Rows
where (int)myRow["RowNo"] == 1
select myRow
It's not that they were deliberately not allowed on DataTables, it's just that DataTables pre-date the IQueryable and generic IEnumerable constructs on which Linq queries can be performed.
Both interfaces require some sort type-safety validation. DataTables are not strongly typed. This is the same reason why people can't query against an ArrayList, for example.
For Linq to work you need to map your results against type-safe objects and query against that instead.
As #ch00k said:
using System.Data; //needed for the extension methods to work
...
var results =
from myRow in myDataTable.Rows
where myRow.Field<int>("RowNo") == 1
select myRow; //select the thing you want, not the collection
You also need to add a project reference to System.Data.DataSetExtensions
I realize this has been answered a few times over, but just to offer another approach:
I like to use the .Cast<T>() method, it helps me maintain sanity in seeing the explicit type defined and deep down I think .AsEnumerable() calls it anyways:
var results = from myRow in myDataTable.Rows.Cast<DataRow>()
where myRow.Field<int>("RowNo") == 1 select myRow;
or
var results = myDataTable.Rows.Cast<DataRow>()
.FirstOrDefault(x => x.Field<int>("RowNo") == 1);
As noted in comments, does not require System.Data.DataSetExtensions or any other assemblies (Reference)
var query = from p in dt.AsEnumerable()
where p.Field<string>("code") == this.txtCat.Text
select new
{
name = p.Field<string>("name"),
age= p.Field<int>("age")
};
the name and age fields are now part of the query object and can be accessed like so: Console.WriteLine(query.name);
Using LINQ to manipulate data in DataSet/DataTable
var results = from myRow in tblCurrentStock.AsEnumerable()
where myRow.Field<string>("item_name").ToUpper().StartsWith(tbSearchItem.Text.ToUpper())
select myRow;
DataView view = results.AsDataView();
//Create DataTable
DataTable dt= new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("ID",typeof(System.Int32)),
new DataColumn("Name",typeof(System.String))
});
//Fill with data
dt.Rows.Add(new Object[]{1,"Test1"});
dt.Rows.Add(new Object[]{2,"Test2"});
//Now Query DataTable with linq
//To work with linq it should required our source implement IEnumerable interface.
//But DataTable not Implement IEnumerable interface
//So we call DataTable Extension method i.e AsEnumerable() this will return EnumerableRowCollection<DataRow>
// Now Query DataTable to find Row whoes ID=1
DataRow drow = dt.AsEnumerable().Where(p=>p.Field<Int32>(0)==1).FirstOrDefault();
//
Try this simple line of query:
var result=myDataTable.AsEnumerable().Where(myRow => myRow.Field<int>("RowNo") == 1);
You can use LINQ to objects on the Rows collection, like so:
var results = from myRow in myDataTable.Rows where myRow.Field("RowNo") == 1 select myRow;
This is a simple way that works for me and uses lambda expressions:
var results = myDataTable.Select("").FirstOrDefault(x => (int)x["RowNo"] == 1)
Then if you want a particular value:
if(results != null)
var foo = results["ColName"].ToString()
Try this
var row = (from result in dt.AsEnumerable().OrderBy( result => Guid.NewGuid()) select result).Take(3) ;
Most likely, the classes for the DataSet, DataTable and DataRow are already defined in the solution. If that's the case you won't need the DataSetExtensions reference.
Ex. DataSet class name-> CustomSet, DataRow class name-> CustomTableRow (with defined columns: RowNo, ...)
var result = from myRow in myDataTable.Rows.OfType<CustomSet.CustomTableRow>()
where myRow.RowNo == 1
select myRow;
Or (as I prefer)
var result = myDataTable.Rows.OfType<CustomSet.CustomTableRow>().Where(myRow => myRow.RowNo);
var results = from myRow in myDataTable
where results.Field<Int32>("RowNo") == 1
select results;
In my application I found that using LINQ to Datasets with the AsEnumerable() extension for DataTable as suggested in the answer was extremely slow. If you're interested in optimizing for speed, use James Newtonking's Json.Net library (http://james.newtonking.com/json/help/index.html)
// Serialize the DataTable to a json string
string serializedTable = JsonConvert.SerializeObject(myDataTable);
Jarray dataRows = Jarray.Parse(serializedTable);
// Run the LINQ query
List<JToken> results = (from row in dataRows
where (int) row["ans_key"] == 42
select row).ToList();
// If you need the results to be in a DataTable
string jsonResults = JsonConvert.SerializeObject(results);
DataTable resultsTable = JsonConvert.DeserializeObject<DataTable>(jsonResults);
Example on how to achieve this provided below:
DataSet dataSet = new DataSet(); //Create a dataset
dataSet = _DataEntryDataLayer.ReadResults(); //Call to the dataLayer to return the data
//LINQ query on a DataTable
var dataList = dataSet.Tables["DataTable"]
.AsEnumerable()
.Select(i => new
{
ID = i["ID"],
Name = i["Name"]
}).ToList();
For VB.NET The code will look like this:
Dim results = From myRow In myDataTable
Where myRow.Field(Of Int32)("RowNo") = 1 Select myRow
IEnumerable<string> result = from myRow in dataTableResult.AsEnumerable()
select myRow["server"].ToString() ;
Try this...
SqlCommand cmd = new SqlCommand( "Select * from Employee",con);
SqlDataReader dr = cmd.ExecuteReader( );
DataTable dt = new DataTable( "Employee" );
dt.Load( dr );
var Data = dt.AsEnumerable( );
var names = from emp in Data select emp.Field<String>( dt.Columns[1] );
foreach( var name in names )
{
Console.WriteLine( name );
}
You can get it work elegant via linq like this:
from prod in TenMostExpensiveProducts().Tables[0].AsEnumerable()
where prod.Field<decimal>("UnitPrice") > 62.500M
select prod
Or like dynamic linq this (AsDynamic is called directly on DataSet):
TenMostExpensiveProducts().AsDynamic().Where (x => x.UnitPrice > 62.500M)
I prefer the last approach while is is the most flexible.
P.S.: Don't forget to connect System.Data.DataSetExtensions.dll reference
you can try this, but you must be sure the type of values for each Column
List<MyClass> result = myDataTable.AsEnumerable().Select(x=> new MyClass(){
Property1 = (string)x.Field<string>("ColumnName1"),
Property2 = (int)x.Field<int>("ColumnName2"),
Property3 = (bool)x.Field<bool>("ColumnName3"),
});
I propose following solution:
DataView view = new DataView(myDataTable);
view.RowFilter = "RowNo = 1";
DataTable results = view.ToTable(true);
Looking at the DataView Documentation, the first thing we can see is this:
Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.
What I am getting from this is that DataTable is meant to only store data and DataView is there enable us to "query" against the DataTable.
Here is how this works in this particular case:
You try to implement the SQL Statement
SELECT *
FROM myDataTable
WHERE RowNo = 1
in "DataTable language". In C# we would read it like this:
FROM myDataTable
WHERE RowNo = 1
SELECT *
which looks in C# like this:
DataView view = new DataView(myDataTable); //FROM myDataTable
view.RowFilter = "RowNo = 1"; //WHERE RowNo = 1
DataTable results = view.ToTable(true); //SELECT *

Categories