Inline select new in LINQ - c#

I have this standard piece of LINQ
var sidEvents = (from f in AppDelegate.Self.Subscription.Fred
from s in f.Sid
from e in s.Eve
where e.date >= oneWeek
select new {Title = e.type, Data = e.data, Date = e.date}).ToList();
I've changed a few things and now I get a list of Eve objects with the where condition after it like this
var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut").Where(t => t.date >= oneWeek).ToList();
Is there a way that I can extend this LINQ query to include the select new {...} that was on the original LINQ query?

var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
.Where(t => t.date >= oneWeek)
.Select(e => new { Title = e.type, Data = e.data, Date = e.date }).ToList();

Maybe you are looking for this:
var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
.Where(t => t.date >= oneWeek)
.Select(e => new {...})

If you are more familiar with the LINQ query syntax, you can
translate the second query from method syntax to query syntax
var sidEvents = (from t in AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
where t.date >= oneWeek
select t).ToList();
and then replace the select t part with your new expression.

Related

Entity framework complex query to nested C# object

I am having following query here. how do I get similar linq query for this sql.
SELECT *
FROM PublishedLineBar
WHERE PublishedRosterShiftId
IN (SELECT LatestShiftId FROM
( SELECT MAX(PublishedRosterShiftId) as LatestShiftId, DayNumber
FROM PublishedRosterShift
WHERE employeeid = 14454
GROUP BY DayNumber)
as ShiftProjection )
I have used below linq translation, but it is failing somewhere.
var shifts = dbContext.PublishedRosterShifts
.Where(h => h.EmployeeId == EmployeeId);
var inner = shifts
.Select(x => new
{
LatestShiftId = shifts.Max(p => p.PublishedRosterShiftId),
DayNumber = x.DayNumber
})
.GroupBy(s => s.DayNumber)
.Select(g => g.FirstOrDefault());
var q = from f in shifts
select new
{
LatestShiftId = shifts.Max(p => p.PublishedRosterShiftId),
DayNumber = f.DayNumber
};
var query = from l in dbContext.PublishedLineBars
where inner.Select(s => s.LatestShiftId).Contains(l.PublishedRosterShiftId)
select l;
Here is the LINQ equivalent of your subquery used for SQL IN (...) clause (with unnecessary nesting removed):
var inner = dbContext.PublishedRosterShifts
.Where(s => s.EmployeeId == EmployeeId)
.GroupBy(s => s.DayNumber)
.Select(g => g.Max(s => s.PublishedRosterShiftId));
and the query using it:
var query = from l in dbContext.PublishedLineBars
where inner.Contains(l.PublishedRosterShiftId)
select l;
or simply
var query = dbContext.PublishedLineBars
.Where(l => inner.Contains(l.PublishedRosterShiftId));
What you are missing in your attempt is that in SQL SELECT MAX(PublishedRosterShiftId) as LatestShiftId, DayNumber operates on the result of the GROUP BY operator, hence in LINQ the Select should be after GroupBy.

Error in Linq query when join multiple table

My linq query is:
var query1 = (from grp in ctnx.tblGroupDatas
group grp by grp.MeterID_FK into g
let maxId = g.Max(gId => gId.GroupDataID)
select new { metId = g.Key, maxId });
query2 = (from met in ctnx.tblMet
from mod in ctnx.tblMod.Where(mo => mo.ModID == met.Mod_FK).DefaultIfEmpty()
from grp in ctnx.tblGroupDatas.Where(gr => gr.Met_FK == met.MetID)
from group1 in db.tblMetRelateGroups.Where(x => x.Met_FK == met.MetID)
from q1 in query1.Where(q => q.metId == met.MetID && grp.GroupDataID == q.maxId)
where (group1.GroupMetID_FK == groupID)
select new
{
met.MetID,
mod.ModSerial,
met.MetSerial,
met.MetWaterSharingNo,
met.MetPowerSharingNo,
grp.GroupDate,
grp.GroupDataID
});
var gridobisdata = query2.OrderByDescending(m => m.GroupDate);
but this show error:
The specified LINQ expression contains references to queries that are
associated with different contexts.

Converting SQL to Linq with groupby, sum and count

I would like to do a group by and on that a sum and a count. I don't seem to be able to create the solution in linq. How can I convert my query to linq?
SELECT HistoricalBillingProductGroup,
COUNT(*),
BillingPeriod,
SUM(TotalMonthlyChargesOtcAndMrc)
FROM [x].[dbo].[tblReport]
group by BillingPeriod, HistoricalBillingProductGroup
order by BillingPeriod
This is what I got sofar in Linq
var result =
context.Reports.GroupBy(x => new {x.BillingPeriod, x.HistoricalBillingProductGroup})
.Select(x => new StatisticsReportLine
{
HistoricalBillingGroup = x.FirstOrDefault().HistoricalBillingProductGroup,
BillingPeriod = x.FirstOrDefault().BillingPeriod,
CountOfRows = x.Count(),
SumOfAmount = x.Sum(p => p.TotalMonthlyChargesOtcAndMrc) ?? 0
})
.ToString();
The query I get from this is enormous and takes a very long time to load. In SQL its a matter of milliseconds. I hardly doubt this is the solution.
I believe the calls to x.FirstOrDefault() are the source of your problem. Each one of these will result in a very costly inner query inside the SELECT clause of the generated SQL.
Try using the Key property of the IGrouping<T> instead :
var result = context.Reports
.GroupBy(x => new {x.BillingPeriod, x.HistoricalBillingProductGroup})
.OrderBy(x => x.Key.BillingPeriod)
.Select(x => new StatisticsReportLine
{
HistoricalBillingProductGroup = x.Key.HistoricalBillingProductGroup,
BillingPeriod = x.Key.BillingPeriod,
CountOfRows = x.Count(),
SumOfAmount = x.Sum(p => p.TotalMonthlyChargesOtcAndMrc) ?? 0
});
Or if you prefer query syntax:
var result =
(from r in context.Reports
group r by new { r.BillingPeriod, r.HistoricalBillingProductGroup } into g
orderby g.Key.BillingPeriod
select new StatisticsReportLine
{
HistoricalBillingProductGroup = g.Key.HistoricalBillingProductGroup,
BillingPeriod = g.Key.BillingPeriod,
CountOfRows = g.Count(),
SumOfAmount = x.Sum(p => p.TotalMonthlyChargesOtcAndMrc) ?? 0
});
You could try this one:
var result = context.Reports
.GroupBy(x => new {x.BillingPeriod, x.HistoricalBillingProductGroup})
.Select(x => new StatisticsReportLine
{
HistoricalBillingGroup = x.Key.HistoricalBillingProductGroup,
BillingPeriod = x.Key.BillingPeriod,
CountOfRows = x.Count(),
SumOfAmount = x.Sum(p => p.TotalMonthlyChargesOtcAndMrc) ?? 0
}).ToString();
In the above query you make a group by on two properties, BillingPeriod and HistoricalBillingProductGroup. So in each group that will be created, you will have a key, that will be consisted by these two properties.

Linq Query with a where condition - count items

How can I change this request :
query = query.Where(item => (from table in context.Table
where table.amount == item.Amount
select table).Count() >= 10);
to not use the subquery (from ... in ...) ?
I tried to create the subquery separately, to use it with the Where condition :
var subQuery = from table in context.Table select table.amount;
var list = subQuery.ToList()
But I don't know how I can use it after that, because of the .Count() operation.
Thank you for your comments.
How about this:
query = query.Where(item => context.Table
.Count(t => t.amount == item.Amount) >= 10);
Or to reduce the number of round-trips:
var counts = context.Table
.GroupBy(t => t.amount)
.Select(g => new {amount = g.Key, count = g.Count()});
query = from q in query
join c in counts
on q.amount equals c.amount
where c.count >= 10
select q;
Just use Count with a predicate directly:
query.Where(item => context.Table.Count(table.amount == item.Amount) >= 10);
what about this one
var subQuery = (from table in context.Table select table.amount).ToList();
query = query.Where(item => subQuery.Count() >= 10);

How can I write the following code more elegantly using LINQ query syntax?

How can I write the following code more elegantly using LINQ query syntax?
var mergedNotes = new List<Note>();
var noteGroupsByUserID = notes.GroupBy( x => x.UserID );
foreach (var group in noteGroupsByUserID)
{
var sortedNotesByOneUser = group.OrderBy( x => x.CreatedOn ).ToList();
var mergedNotesForAUserID = GetMergedNotesFor( sortedNotesByOneUser );
mergedNotes.AddRange( mergedNotesForAUserID );
}
return mergedNotes;
Not LINQ syntax, but at least more elegant...
List<Note> mergedNotes =
notes
.GroupBy(x => x.UserID)
.SelectMany(g => GetMergedNotesFor(g.OrderBy(x => x.CreatedOn)))
.ToList();
With my test data it creates the same result as your original code.
I think this does the trick:
var mergedNotes = new List<Note>();
mergedNotes.AddRange((from n in notes
orderby n.CreatedOn
group n by n.UserID into g
let m = GetMergedNotesFor(g)
select m).SelectMany(m => m));
return mergedNotes;

Categories

Resources