multiple group by using linq - linq

I need return just 2 lines in my query. One line with a string Today and a number of cases closed today, on my second line I need a string Last Week and a number of cases closed on the last week.
How I group with a range date?
Sum Name
----------- ----------
12 Today
33 Last Weeb

How about this:
var caseCounts = Cases
.Where(c => c.Date == today || (c.Date >= startOfLastWeek && c.Date <= endOfLastWeek))
.GroupBy(c => c.Date == today ? "Today" : "Last Week")
.Select(g => new {
Name = g.Key, Sum = g.Count()
});
You would need to define the 3 dates (today, startOfLastWeek, endOfLastWeek) before hand, but it gives you the results you are after.

GROUP BY YEARWEEK(date) should work. Depending on your dbms, you might be able to use another function, or program your own.
http://www.tutorialspoint.com/sql/sql-date-functions.htm#function_yearweek

Related

How to make zero counts show in LINQ query when getting daily counts?

I have a database table with a datetime column and I simply want to count how many records per day going back 3 months. I am currently using this query:
var minDate = DateTime.Now.AddMonths(-3);
var stats = from t in TestStats
where t.Date > minDate
group t by EntityFunctions.TruncateTime(t.Date) into g
orderby g.Key
select new
{
date = g.Key,
count = g.Count()
};
That works fine, but the problem is that if there are no records for a day then that day is not in the results at all. For example:
3/21/2008 = 5
3/22/2008 = 2
3/24/2008 = 7
In that short example I want to make 3/23/2008 = 0. In the real query all zeros should show between 3 months ago and today.
Fabricating missing data is not straightforward in SQL. I would recommend getting the data that is in SQL, then joining it to an in-memory list of all relevant dates:
var stats = (from t in TestStats
where t.Date > minDate
group t by EntityFunctions.TruncateTime(t.Date) into g
orderby g.Key
select new
{
date = g.Key,
count = g.Count()
}).ToList(); // hydrate so we only query the DB once
var firstDate = stats.Min(s => s.date);
var lastDate = stats.Max(s => s.date);
var allDates = Enumerable.Range(1,(lastDate - firstDate).Days)
.Select(i => firstDate.AddDays(i-1));
stats = (from d in allDates
join s in stats
on d equals s.date into dates
from ds in dates.DefaultIfEmpty()
select new {
date = d,
count = ds == null ? 0 : ds.count
}).ToList();
You could also get a list of dates not in the data and concatenate them.
I agree with #D Stanley's answer but want to throw an additional consideration into the mix. What are you doing with this data? Is it getting processed by the caller? Is it rendered in a UI? Is it getting transferred over a network?
Consider the size of the data. Why do you need to have the gaps filled in? If it is known to be returning over a network for instance, I'd advise against filling in the gaps. All you're doing is increasing the data size. This has to be serialised, transferred, then deserialised.
If you are going to loop the data to render in a UI, then why do you need the gaps? Why not implement the loop from min date to max date (like D Stanley's join) then place a default when no value is found.
If you ARE transferring over a network and you still NEED a single collection, consider applying D Stanley's resolution on the other side of the wire.
Just things to consider...

LINQ get Max value from list

I have the following table:
ID Amt Received
-- ---- --------
2 55 N
2 88 Y
2 44 N
3 5 N
3 9 N
4 5 N
5 33 Y
6 43 N
7 54 N
var result = (from rs in db.Exp
where rs.ID == id
&& rs.Received == true
select rs).Max().Any();
Given an ID, I need to find the max Amt for a given id and then check if it is Y, if so, return true else return false.
This should do it;
db.Exp.
Where(x => x.ID == id).
OrderByDescending(x => x.Amt).
Take(1).
Any(x => x.Received == "Y");
Unfortunately LINQ doesn't provide a "max by an attribute" method. MoreLINQ does with its MaxBy operator, but that can't be translated into SQL of course. So if this is a LINQ to SQL (or whatever) query, you'll need a different approach. If it's already LINQ to Objects, however:
return db.Exp.Where(rs => rs.ID == id)
.MaxBy(rs => rs.Amt)
.Received;
Note that this is doing what the words of your question ask:
Out of the records with the given ID...
Find the one with the highest amount...
And check the value of Received
This is not the same as:
Out of the records with the given ID where received is true...
Find the one with the highest amount
Also note that this will throw an exception if there are no records with that ID.
If you want to do it in LINQ to SQL etc, you'd probably be best off with an ordering:
var highest = db.Exp.Where(rs => rs.ID == id)
.OrderByDescending(rs => rs.Amt)
.FirstOrDefault();
return highest != null && highest.Received;
You don't want to do this if you're using LINQ to Objects, as it will order all the results, when you only want to find the result with the highest amount.
You need to tell it what you want the Max of.
var result =
(from rs in db.Exp
where rs.ID == id && rs.Received
select rs)
.Max(row => row.Amt) == Y;
And you don't need the .Any() at all
// "I need to find the max Amt for a given id..."
var elementWithMaxAmount =
db.Exp
.Where(x => x.ID == id)
.OrderByDescending(x => x.Amount)
.FirstOrDefault();
// "... and then check if it is Y"
var result = (elementWithMaxAmount != null &&
elementWithMaxAmount.Received == "Y");

LINQ - how to get next three business days excluding dates in table?

I need to find the next three available business days for a scheduling application. What is available depends upon excluded dates in a table. So...as long as the date is not in my table and not a Saturday or Sunday, I want the next three. I'd like to find an efficient way of doing this.
I need to return a List<DateTime>. The table is simple - ExcludedDates has an ID and a DateTime with the excluded date.
I'd like to have a single LINQ query expression but can't figure it out...thanks to all in advance and I apologize if this is trivial or obvious - it isn't to me.
Try this...
DateTime start = DateTime.Now.Date;
var result = Enumerable.Range(1, 10) // make this '10' higher if necessary (I assume you only exclude non-workingdays like Christmas and Easter)
.Select(offset => start.AddDays(offset))
.Where(date => !( date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek== DayOfWeek.Sunday))
.Where(d=> !exceptionTable.Any(date => date == d))
.Take(3).ToList();
List<DateTime> result = (from i in Enumerable.Range(1, excludeTable.Rows.Count + 6)
let date = inputDate.AddDays(i)
where date.DayOfWeek != DayOfWeek.Saturday &&
date.DayOfWeek != DayOfWeek.Sunday &&
!excludeTable.Rows.Cast<DataRow>().Select(r => (DateTime) r["ExcludeDate"]).Contains(date)
select date).Take(3).ToList();
excludeTable.Rows.Count + 6 is to cover the worst case where you skip over every thing in the excludeTable and then you have to skip over another weekend.
This assumes that a month will be reasonable depending on your excluded dates.
DateTime date = DateTime.Today;
// first generate all dates in the month of 'date'
var dates = Enumerable.Range(1, DateTime.DaysInMonth(date.Year, date.Month)).Select(n => new DateTime(date.Year, date.Month, n));
// then filter the only the start of weeks
var results = (from d in dates
where d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Saturday && !excludes.Any(i => i.DateTime.Date == d.Date) && date < d
select d).Take(3);
var excludedList = new List<long>() { DateTime.Parse("2011-07-27").Ticks };
var week = new List<long>(){
DateTime.Now.Date.Ticks,
DateTime.Now.Date.AddDays(1).Ticks,
DateTime.Now.Date.AddDays(2).Ticks,
DateTime.Now.Date.AddDays(3).Ticks,
DateTime.Now.Date.AddDays(4).Ticks,
DateTime.Now.Date.AddDays(5).Ticks,
DateTime.Now.Date.AddDays(6).Ticks,
DateTime.Now.Date.AddDays(7).Ticks,
DateTime.Now.Date.AddDays(8).Ticks
};
var available = (from d in week.Except(excludedList)
where new DateTime(d).DayOfWeek != DayOfWeek.Saturday && new DateTime(d).DayOfWeek != DayOfWeek.Sunday
select new DateTime(d)).Take(3);
foreach (var a in available)
Console.WriteLine(a.ToString());

How to get sum in nested collection via LINQ

class YearlyData
{
MonthlyData[] monthlyData = new MonthlyData[12];
}
class MonthlyData
{
int Salary;
}
Given I have a List<YearlyData>, how can I find total salary for a given month for the number of years.
Example for three years I need total salary given for 1st month & subsequent months.
If you want the sum of all the Salaries for April:
List<YearlyData> l;
l.Sum(yd => yd.monthlyData[3].Salary);
If I misread your question, and you really want all the salaries for April & subsequent months (in the year)
l.Sum(yd => yd.monthlyData.Skip(3).Sum());
Something like this:
list.SelectMany(x => x.monthlyData
.Select((m, i) => new {month = i+1,
data = m.Salary}
))
.GroupBy(x => x.month)
.Select(x => new {month = x.Key,
total = x.Sum(m => m.data)});
This will give you a list with twelve entries, one for each month along with the total amount of that month.

LINQ: How to perform a conditional sum?

I have a LINQ Query that creates a new type that contains a days of week and a sum of hours worked.
My current (incorrect query) looks like this:
var resultSet = (from a in events
group a by a.Start.DayOfWeek into g
select new DaySummary
{
day = g.Key.ToString(),
hoursWorked = g.Any(p => p.Title == "Lunch") ? 0 :
Math.Round((g.Sum(
p => (Decimal.Parse((p.End - p.Start).TotalMinutes.ToString()))) / 60), 2)
}).ToList();
Hopefully you can see what Im trying to do. The Any method is not having the effect I'd like however. Basically I want to to sum up the hours worked, but if the title was "lunch" I want it to add 0.
The logic of this is just a little beyond me at the moment.
UPDATE
Ok, Im an idiot. Changes the query to this and it now works. Sorry.
var resultSet = (from a in events
group a by a.Start.DayOfWeek into g
select new DaySummary
{
day = g.Key.ToString(),
hoursWorked = Math.Round((g.Where(p => p.Title !="Lunch").
Sum(p => (Decimal.Parse((p.End - p.Start).TotalMinutes.ToString()))) / 60), 2)
}).ToList();
It seems each group is a sequence of 'periods' and you just want to ignore any 'lunch' periods in each calculation. In that case you just need to remove these from the sum using Where.
var hours = events
.GroupBy(e => e.Start.DayOfWeek)
.Select(g => new DaySummary {
day = g.Key.ToString(),
hoursWorked = Math.Round(
g.Where(p => p.Title != "Lunch")
.Sum(pd => (Decimal.Parse((pd.End - pd.Start).TotalMinutes.ToString())) / 60), 2)
}).ToList();

Resources