I am connecting my Windows Form app to their Access DB (ugh, I know), and cannot get my linq query to return anything.
var matchDateField = from myRow in boilerDT.AsEnumerable()
where myRow.Field<DateTime>("EntryDate").ToShortDateString() == dateTimePicker1.Value.ToShortDateString()
select myRow;
Any suggestions?
Get all the rows:
IEnumerable<DataRow> dateFieldQuery =
from myRow in boilerDT.AsEnumerable()
select myRow;
Filter by date:
IEnumerable<DataRow> matchDateField =
dateFieldQuery.Where(p => p.Field<DateTime>("EntryDate").Date == dateTimePicker1.Value);
So here you're using deferred execution which enables multiple queries to be combined or a query to be extended. When a query is extended, it is modified to include the new operations, and the eventual execution will reflect the changes.
The first query returns all the rows and the second query extends the first by using Where to return all the rows with specific date.
Related
How to create DataTable in LINQ? In SQL Statements i used
DataTable dt;
dt = con.openDataTableQuery("SELECT TransactionID FROM HeaderTransaction");
How about in LINQ? Can you give an example?
Please see the documentation for LINQ to DataSet, examples can be found on the page.
The following is an example emulating your SQL code.
// Add a reference to System.Data.DataSetExtensions
// Query the headerTransaction table to select all items with TransactionID == 1, as an example, use your own value(s)
IEnumerable<DataRow> query =
from headers in headerTransaction.AsEnumerable()
where order.Field<int>("TransactionID") = 1
select order;
// Create a table from the query.
DataTable headersIdQuery = query.CopyToDataTable<DataRow>();
AsEnumerable() returns IEnumerable. As you asked for a DataTable, we convert IEnumerable to a DataTable, using CopyToDataTable().
I am getting data like 200k records from the database and store it in a linq result with a ColumnName EMAIL. Now,I want to show all emails from the linq result and adding them to a TextBox by separating with a ,.
Actually,I have prepared DataTable with that linq result and have combined all row data with the code :
var dataLists = (from xx in VDC.SURVEY_EMAIL_LIST
where xx.EMAIL_GROUP_ID == ListGroupID
select xx).ToList();
DataTable DtDataLists = LINQToDataTable(dataLists);
EmailIDS = string.Join(",", DtDataLists.AsEnumerable().Select(x => x["EMAILID"].ToString()).ToArray());
But,for preparing DataTable,it is taking a long time.
So,I thought of preparing the string EmailIDS directly from the linq result.
Can anyone help me?
This code should work for you but I'm not sure that it'll be much faster:
string.Join(",", dataLists.Select(x => x.EMAILID));
I want to apply the follwing Sql query in my DataTable
SELECT MakeDistinct AS AfterDistinct
, COUNT(MakeDistinct) AS Count
FROM MyTable
GROUP BY MakeDistinct
Refer this Question for more details
something like:
var query = from row in table.AsEnumerable()
group row by row.Field<int>("MakeDistinct") into grp
select new {AfterDistinct = grp.Key, Count = grp.Count()};
foreach(var row in query) {
Console.WriteLine("{0}: {1}", row.AfterDistinct, row.Count);
}
Note that aggregating at the database server will usually be much more efficient than populating a DataTable over the network and then aggregating the DataTable.
You are partially looking for DataTable.Compute. That method can calculate aggregate functions for you. So you get something like:
object sumObject;
sumObject = myDataTable.Compute("Sum(Count)", ""); // second parameter is the where clause
For grouping by columns, see this question: Efficient DataTable Group By. It provides a Linq implementation as well as a 'non-Linq' implementation.
Use System.Data.DataSetExtensions and try something like this
var result = from row in dt.AsEnumerable()
group row by row.Field<int>("MakeDistinct") into grp
select new
{
MakeDistinct = grp.Key,
Count = grp.Count()
};
Just started to play around with datatable and LINQ today.
I have a datatable that gets a list of Names From SQL database.
I am looking to return a specific name from the dt using LINQ.
I have tried the following code with no success at this. Is there something that i am doing wrong with the code.
dt returns a full list of names i am just looking to reduce the names down to one name. There is a name in the adventureworks database called Blade i am trying to display this only.
DataTable dt = DAL.GetNames();
try
{
var q = from myrow in dt.AsEnumerable()
where myrow.Field<string>("Name") =="Blade"
select myrow;
dataGridView1.DataSource = q;
}
I have tried to replace the == with a .equals.
I am totally new to the concept of using a Language intergrated query.
when i run the code noting happens i dont get any errors ect just no data returned.
You're defining your query but not actually running it.
Your line:
dataGridView1.DataSource = q;
Needs to be:
dataGridView1.DataSource = q.AsDataView();
I have an ADO.NET dataset which is set by a certain query,
say
SELECT ID,USER,PRODUCT,COUNT FROM PRODUCTION
Without using a where clause I need to derive some results from the dataset. Say I want to get the User and Product count of the user who has the maximum product count. (And I want to do it by using the existing dataset. I can't derive this from dataset.)
Any idea of a way to query inside the dataset? Since there are Datatables my thought was there is some way to query it.
Traditional SQL queries cannot be applied to the DataSet. The following is possible, however:
Filter rows using DataTable.Select. See here for detailed information about expressions in DataTables.
Calculate totals etc. using DataTable.Compute.
If these two don't do the trick, there's always LINQ.
Quick-and-dirty LINQ example: (which doesn't return a DataTable, but a list containing an anonymous type):
var joinedResult = dataTable1
// filtering:
.Select("MyColumn = 'value'")
// joining tables:
.Join(
dataTable2.AsEnumerable(),
row => row.Field<long>("PrimaryKeyField"),
row => row.Field<long?>("ForeignKeyField"),
// selecting a custom result:
(row1, row2) => new { AnotherColumn = row1.Field<string>("AnotherColumn") });
AsEnumerable converts a DataTable into an IEnumerable on which LINQ queries can be performed. If you are new to LINQ, check out this introduction.
Yes, you can use DataTable.Select method.
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
Also see this link.
You can do cross-table queries of a Dataset object using LINQ to DataSet:
msdn.microsoft.com/en-us/library/bb386969.aspx