I have the following linq query
var temp = (from p in db.BEM_EVT_FULL
where (p.date_reception > dt)
group p by p.mc_object into g
orderby g.Count() descending
select new StringIntType
{
str = g.Key,
nbr = g.Count()
}).Take(50).ToList();
Instead of casting result to StringIntType i need to use Dictionary with int and string types.
You could try something like this:
var temp = (from p in db.BEM_EVT_FULL
where (p.date_reception > dt)
group p by p.mc_object into g
orderby g.Count() descending
select new
{
Key = g.Key,
Value = g.Count()
}).Take(50)
.ToDictionary(x=>x.Key, y=>y.Value);
it looks like there may be an answer for this question here: Convert Linq Query Result to Dictionary
basically, instead of ToList use ToDictionary
.ToDictionary( t => t.Key, t => t.Value);
Related
I'm generating a pie chart, currently I have have the following variable, How to exclude Null records? it works but it includes null values in the pie chart
var PieChartData1 = from T1 in Result
group T1 by T1.Reasons into G1
orderby count ascending
select new { G1.Key, Count = G1.Count() };
obj.peichart1 = PieChartData1.ToArray();
if T1 is Nullable use HasValue Property
var PieChartData1 = from T1 in Result
where T1.HasValue
group T1 by T1.Reasons into G1
orderby count ascending
select new { G1.Key, Count = G1.Count() };
Add a where clause to filter the null values before you use them - here I'm assuming that either T1 or T1.Reasons can be null:
var PieChartData1 = from T1 in Result
where T1 != null && T1.Reasons != null
group T1 by T1.Reasons into G1
orderby count ascending
select new { G1.Key, Count = G1.Count() };
I also suspect that orderby count ascending should be orderby G1.Count() ascending.
You can also write it using lambda expressions:
var pieChartData = Result.Where(r => r.Reason != null)
.GroupBy(r => r.Reason)
.OrderBy(g => g.Count())
.Select(g => new { g.Key, Count = g.Count() });
I have some linq which returns a list of properties and the number of bookings for a given year. However if a property has no bookings then it is not included in the resultset.
var bookings = from b in db.Bookings
orderby b.PropertyId
where b.StartDate.Year == Year
group b by b.Property.Title into grp
select new { key = grp.Key, cnt = grp.Count() };
How can this be changed to include properties with no bookings?
I'm assuming there's a Properties table based on your code. You need to select from Properties instead:
var bookings = from p in db. Properties
orderby p.Id
group p by p.Title into grp
select new
{
key = grp.Key,
cnt = grp.Count(p => p.Bookings.Where(b => b.StartDate.Year == Year))
};
I guess you should filter on year in the count, then
var bookings = from b in db.Bookings
orderby b.PropertyId
group b by b.Property.Title into grp
select new {
key = grp.Key,
cnt = grp.Count(x => x.StartDate.Year == Year)
};
I need extra where clause for my Linq query. For example if customer choose a date filter so i need to date filter to my query etc... When i try to myQuery.Where predicate there is visible just group by's field.
How can i append new where condition to my query.
//for example i need dynamically append o.OrderDate==Datetime.Now or another where clause
var myQuery =(from o in _db.Orders
join l in _db.OrderLines.Where(x => x.ParaBirimi == model.ParaBirimi) on o.orderId equals
l.OrderId
where o.OrderDate.Value.Year == year1
group o by new {o.OrderDate.Value.Month}
into g
select
new
{
Month = g.Key.Month,
Total = g.Select(t => t.OrderLines.Sum(s => s.OrderTotal)).FirstOrDefault()
});
You are too late at the end of the query to add new Where. You have already grouped the data, and projected it, removing nearly all the fields.
Try:
var baseQuery = from o in _db.Orders
join l in _db.OrderLines.Where(x => x.ParaBirimi == model.ParaBirimi) on o.orderId equals l.OrderId
where o.OrderDate.Value.Year == year1
select new { Order = o, OrderLine = l };
if (something)
{
baseQuery = baseQuery.Where(x => x.Order.Foo == "Bar");
}
var myQuery = (from o in baseQuery
group o by new { o.Order.OrderDate.Value.Month }
into g
select
new
{
Month = g.Key.Month,
Total = g.Sum(t => t.OrderLine.OrderTotal)
});
Clearly you can have multiple if. Each .Where() is in && (AND) with the other conditions.
Note how the result of the join is projected in an anonymous class that has two properties: Order and OrderLine
I have the following sql, which I want to convert to linq
SELECT Contrato.finca, SUM(Pago.Importe_subtotal)
FROM Pago, Contrato
WHERE Pago.Contrato = Contrato.ID AND Pago.pagado = 1
GROUP BY Contrato.finca
ORDER BY 2 DESC
GO
What I have now in linq is the following, but the group by doesn't work.
var x = from contrato in ctx.Contratos
join pago in ctx.Pagos
on contrato.ID equals pago.Contrato
where pago.pagado == true
group contrato by contrato.finca
select contrato.Finca1;
Think this should work:
ctx.Pagos.Where(m=>m.pagado==true).GroupBy(m=>m.Contrato.finca)
.Select(m=> new { Id= m.Key, Val= m.Sum(n => n.Importe_subtotal)})
Try
var x = from contrato in ctx.Contratos
join pago in ctx.Pagos
on contrato.ID equals pago.Contrato
where pago.pagado == true
group new {contrato, pago} by contrato.finca into g
select new {
key = g.Key,
sum = g.Sum(p => p.pago.Importe_subtotal)
};
I have the following LINQ query:
var query =
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
select new { MyObjectID = g.Key, totalSum })
.Max(g => g.totalSum);
I want to select both the object id and price of the object with the maximum price. How can I do that?
Use an order by descending clause and call FirstOrDefault().
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
orderby totalSum descending
select new { MyObjectID = g.Key, totalSum }).FirstOrDefault();