Linq (MIN && MAX) - linq

What is equal of below sql in LINQ
select MIN(finishTimestamp) AS FromDate, MAX(finishTimeStamp) AS ToDate From Transactions
??
from t in Transactions
select new {
FromDate = ?,
ToDate = ?
}
Thanks

To use multiple aggregates in Linq to SQL, on a table, without grouping, the only way I've found to avoid doing multiple queries, is to make a "fake group":
var q = from tr in dataContext.Transactions
group tr by 1 into g // Notice here, grouping by a constant value
select new
{
FromDate = g.Min(t => t.InvoiceDate),
ToDate = g.Max(t => t.InvoiceDate)
};
Kinda hacky, but the generated SQL is clean, and by doing so, you make only one query to the database.

You can just do
var transactionDates = from t in Transactions
select t.FinishTimeStamp;
var dates = new {
FromDate = transactionDates.Min(),
ToDate = transactionDates.Max()
};

You can also use the aggregate functions if you want (Example in VB)
Dim max = Aggregate tMax In Transactions Select tMax Into Max()

Related

ASP.net LINQ on DataView to use Like query

I want to filter a DataView but DV.Rowfilter is taking too much time .
dv.RowFilter = "ProductName like '%" + searchtxt + "%'";
So I decided to use the LINQ but how to implement above like query in LINQ ?
LINQ is not more efficient in general, but it can increase readability and maintainability.
So you can try Linq-To-DataSet:
var query = from row in dv.Table.AsEnumerable()
let productName = row.Field<string>("ProductName")
where productName.Contains(searchtxt)
select row;
DataTable tbl = query.CopyToDataTable(); // use this as DataSource or use tbl.DefaultView
Here the same with method syntax:
var query = dv.Table.AsEnumerable()
.Where(row => row.Field<string>("ProductName").Contains(searchtxt));
MSDN: Creating a DataView Object with LINQ to DataSet
i have tried your second solution, but now its throwing the exception
"The source contains no DataRows." and actually the DataTable which i
make as DataTable.AsEnumerable() , it has the rows in it
The table contains rows but the filter skips all of them.
You could use if(query.Any()){...} to check if there are rows:
DataTable tbl = dv.Table.Clone(); // creates an empty table with the same columns
if(query.Any())
tbl = query.CopyToDataTable();

row number with help of linq

my table consists of 3 columns(sno,name,age) now i am retrieving this table from the database with extra column(row number) ,i used the following code
select * from (
select ROW_NUMBER() over (order by SNo asc)as rowindex,SNo,Name,Age
from tblExample)
as example where rowindex between ((pageindex*10)+1) and ((pageindex+1)*10)
note:here pageindex is the varaible that takes some intger value which is passed by the user
my data base is sql server 2008, now i want to write the same query using linq
can any one please change the abovesql query into linq. iam unable to do it as iam new to linq. iam struck up with this problem please help me thank you in advance
You can write query as beow
var index=1;
var pageIndex=1;
var pageSize = 10;
data.Select(x => new
{
RowIndex = index++,
Sno = x.Sno,
Name = x.Name,
Age = x.Age
}).OrderBy(x => x.Name)
.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();

Linq query with two sub-queries that group by, one with an average, and one with a max

I have a parent table, parentTable which may or may not have children in childTable. I am looking to get average % complete of any given parent's children, and the MAX(due) (date) of the children where they exist. My SQL is this:
SELECT parentRecord_id, assigned_to,
(SELECT avg(complete)
FROM childTable
WHERE parent_id = parentRecord_id
and deleted IS NULL
GROUP BY parent_id),
(SELECT max(due)
FROM childTable
WHERE parent_id = parentRecord_id
and deleted IS NULL
GROUP BY parent_id
)
FROM parentTable s
WHERE s.deleted IS NULL and assigned_to IS NOT NULL
My result set gives me rows with either correct values for the average and max, or null. In this instance I have to do follow up processing so I could ignore the null values if I was doing a foreach through DataTable rows. However I am trying to do this in Linq and can't figure out how to avoid a System.InvalidOperationException where Linq is trying to cast null to a double. Here is what I've tried so far.
var query8 = from s in db.parentTable
where s.deleted == null
select new
{
ID = s.assigned_to,
Average =
((from t in db.childTable
where t.parent_id == s.strategy_id
group t by new { t.parent_id } into g
select new
{
a0 = g.Average(f0 => f0.complete )
}).FirstOrDefault().a0)
};
foreach (var itm in query8)
{
Console.WriteLine(String.Format("User id:{0}, Average: {1}", itm.ID, itm.Average));
}
Here's my question. How do I get the query to handle those returned rows where average complete or max due (date) are null?
You can either filter out the records where the values are null (by another condition) or if you want to include them do something like this:
a0 = g.Average(f0 => f0.complete.HasValue? f0.complete: 0 )
I would cast the list to nullable double before calling Average/Max like so:
var query8 =
from s in db.parentTable
where s.deleted == null
select new
{
ID = s.assigned_to,
Average =
from t in db.childTable
where t.parent_id == s.strategy_id
group t by t.parent_id into g
select g.Cast<double?>().Average(f0 => f0.complete)
};
Assuming complete is a Nullable, you should be able to do:
var query8 = from s in db.parentTable
where s.deleted == null
select new
{
ID = s.assigned_to,
Average =
((from t in db.childTable
where t.parent_id == s.strategy_id
&& s.complete.HasValue()
group t by new { t.parent_id } into g
select new
{
a0 = g.Average(f0 => f0.complete )
}).FirstOrDefault().a0)
};
Thanks to all who responded.
I was unable get around the null anonymous issue with the basic query as I had it, but adding a join to the childTable eliminated the nulls.
Another solution is to use a from x in g.DefaultIfEmpty clause.
var query8 =
from st in db.tableParent
select new { Ass = st.assigned_to ,
Avg =
(from ta in db.tableChild
group ta by ta.parent_id into g
from x in g.DefaultIfEmpty()
select g.Average((f0=>f0.complete))).FirstOrDefault()
};

Linq databinding issue....data doesnt show up in gridview ...I get the message that number and name field are not found in the data source

List<Emp> employees = new List<Emp>();
Emp e1 = new Emp();
e1.number = 2;
e1.name = "Dinesh";
employees.Add(e1);
Emp e2 = new Emp();
e2.number = 3;
e2.name = "Vishal";
employees.Add(e2);
var query = from n in employees
orderby n.name descending
select n;
GridView1.DataSource = query;
GridView1.DataBind();
You should try to debug the situation as possibly some settings in GridView are not exactly matching DataSource.
Easily you could bind your grid in design time to type Emp, which would generate all the columns for you:
In YourGridFile.Designer.cs add:
this.GridView1.AutoGenerateColumns = true;
this.GridView1.DataSource = typeof(Emp);
somewhere before ResumeLayout lines.
Then you will be able to compare what you have set up manually and what should be in columns definitions.

Entity Framework T-Sql "having" Equivalent

How can I write a linq to entities query that includes a having clause?
For example:
SELECT State.Name, Count(*) FROM State
INNER JOIN StateOwner ON State.StateID = StateOwner.StateID
GROUP BY State.StateID
HAVING Count(*) > 1
Any reason not to just use a where clause on the result?
var query = from state in states
join stateowner in stateowners
on state.stateid equals stateowner.stateid
group state.Name by state.stateid into grouped
where grouped.Count() > 1
select new { Name = grouped.Key, grouped.Count() };
I believe you can use a GroupBy followed by a Where clause and it will translate it as a Having. Not entirely sure though.
If you want to compare a variable that is not in the group by (Ex: age), then it would be:
var duplicated = (
from q1 in db.table1
where (q1.age >= 10 )
group q1 by new { q1.firstName, q1.lastName } into grp
where (grp.Count() > 1 )
select new
{
firstName= grp.Key.firstName,
lastName = grp.Key.lastName,
}
);

Resources