I want to get the list of sales for each month ordered by product.
Currently I have the following linq query:
var query = storeDB.OrderDetails
.Select(od => od)
.GroupBy(r => r.Product)
.Select(group => new ProductSalesModel {
Product = group.Key,
Sales = group.Sum(s => s.Quantity),
Amount = group.Sum(s => s.Quantity * s.UnitPrice)
})
.OrderByDescending(x => x.Amount);
How should I do to also group the list by month getting the sales date from my Order table ? Order.CreationDate?
I don't know what Linq provider you are trying to use but in plain Linq this should work:
.GroupBy(r => new {r.Product, r.CreationDate})
and then when you select:
.Select(group => new ProductSalesModel {
Product = group.Key.Product,
CreationDate = group.Key.CreationDate,
Sales = group.Sum(s => s.Quantity),
Amount = group.Sum(s => s.Quantity * s.UnitPrice)
})
If you want to order by something it needs to be in the grouping.
Then you can use OrderByDescending(t=> t.amount).ThenBy(y => y.OrderDate)
Related
I have a table with 3 columns.
And I would like to present a table with this structure:
Can someone show me how to do this with Lambda expressions?
So far I've only gotten the result if I only wanted to show one column:
var sum_data = _context.HechosFinanza
.Where(x => x.Product.Sale_Type == "Cash Sale")
.GroupBy(x => x.Product.Product_Name)
.Select(x => Product { Tienda = x.Key, Total = x.Sum(s =>
s.sales_amount) });
I don't know if something like this may be possible (no idea really, just trying to figure it out):
var sum_data = _context.HechosFinanza
// I remove there where condition from here
.GroupBy(x => x.Product.Product_Name)
// And I add the where condition in each sum
.Select(x => Product { Tienda = x.Key,
TotalCash = x.Sum(s => s.sales_amount).Where(s => s.Product.Sale_Type == "Cash Sale"),
TotalCredit = x.Sum(s => s.sales_amount).Where(s.Product.Sale_Type == "Credit Sale")
});
Uhm, well. It turns out I was really close.
Just had to put the 'Where' statement before.
Answer:
var sum_data = _context.HechosFinanza
// I remove there where condition from here
.GroupBy(x => x.Product.Product_Name)
// And I add the where condition in each sum
.Select(x => Product { Tienda = x.Key,
TotalCash = x.Where(s => s.Product.Sale_Type == "Cash Sale").Sum(s => s.sales_amount),
TotalCredit = x.Where(s.Product.Sale_Type == "Credit Sale") .Sum(s => s.sales_amount)
});
And done.
I have table structure like
Product {List<Cost> Costs}
Cost{List<Invoice> Invoices, Product product}
Invoice{bool isIncluded}
Need a query to get all Products which has any Cost for which none of invoice is included (isIncluded=false for all)
I tried something like:
Product pro= null;
Product p = null;
var costQuery = QueryOver.Of<Cost>()
.JoinAlias(c => c.Product, () => p)
.Where(() => p.Id == pro.Id)
.WhereNot(c=>c.Invoices.Any(i=>i.IsIncluded))
.Select(c => c.Id);
var query = CurrentSession.QueryOver<Product>(() => pro)
.WithSubquery.WhereExists(costQuery);
Use of 'Any' in query errors out:
Unrecognised method call: System.Linq.Enumerable:Boolean
Any[Invoice](System.Collections.Generic.IEnumerable1[Trigger.StageGate.Services.BusinessEntities.Invoice],
System.Func2[Trigger.StageGate.Services.BusinessEntities.Invoice,System.Boolean])
try:
var costQuery = QueryOver.Of<Cost>()
.JoinAlias(c => c.Product, () => p)
.Where(() => p.Id == pro.Id)
.JoinQueryOver<Invoice>(c => c.Invoices)
.WhereNot(i => i.IsIncluded)
.Select(c => c.Id);
I use the following LINQ to create grid model:
...
var query = from a in GetUser()
select new UserModel
{
ID = a.ID,
StartDate = a.StartDate,
EndDate = a.EndDate,
StateName = a.State.StateName,
StateID = a.StateID,
City = a.City,
};
return query;
....
and the HTML is
#Html.Grid(Model.PagedList).Columns(
col =>
{
col.For(c => c.StateName).Named("State").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.City).Named("City").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.StartDate).Named("Start Date").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => c.EndDate).Named("End Date").Attributes(#class => "row").HeaderAttributes(#class => "head");
col.For(c => Html.ActionLink("Details", "Details", new { id = c.ID })).Named("View Details").HeaderAttributes(#class => "head").DoNotEncode();
}).Sort(Model.GridSortOptions).Attributes(#class => "grid")
The main problem is how to show only DATE without TIME part?
I tried some options but in some cases I got the errors and in other cases sorting AZ doesn't work at all.
Any clue?
Thank you!!!
Try using
col
.For(c => c.EndDate.ToLongDateString())
.Named("End Date")
.Attributes(#class => "row")
.HeaderAttributes(#class => "head");
Something a little easier I find is the Format method on the column. see docs here
So your code looks something like this:
col.For(c => c.EndDate).Format("{0:d"})
//use .Format("{0:yyyy-MM-dd}") to get 2014-10-21
If you want to use ToShortDateString then you will need to define the column name as well as the sort column name, for example:
col.For(c => c.EndDate.ToShortDateString())
.Named("End Date")
.SortColumnName("EndDate");
how can i show null or empty fields in last by apply Orderby:
here is my code.
Var movies = _db.Movies.Orderby(c => c.Category).ThenBy(n => n.Name)
If I understand your question correctly:
Var movies = _db.Movies
.OrderBy(c => c.Category==null?1:0)
.Thenby(c => c.Category).ThenBy(n => n.Name)
I have a business object structured like this:
Country has States, State has Cities
So Country[2].States[7].Cities[5].Name would be New York
Ok, I need to get a list of all the Country objects which have at least 1 City.IsNice == true
How do I get that?
var selectedCountries =
countries.Where(
co => co.States.Any(
s => s.Cities.Any(
ci => ci.IsNice)));
Another option :
var selectedCountries =
countries.Where(
co => co.States.SelectMany(s => s.Cities).Any(
ci => ci.IsNice));
var result = (from country in db.Countries
from state in country.States
from city in state.Cities
where city.IsNice
select county).Distinct();
I would do it in one of two ways:
var selectedCountries = from country in countries
from state in country.States
from city in state.Cities
where city.IsNice
select country;
or
var selectedCountries =
countries.Where(country =>
country.States.FirstOrDefault(state =>
state.Cities.FirstOrDefault(city =>
city.IsNice) != null) != null);
var result = Countries
.SelectMany(a => a.States)
.SelectMany(b => b.Cities)
.Where(b => b.IsNice == true)
.ToList();