I have a product that have list of price, Can i order by price?
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public IList<ProductPrice> ProductPrices{ get; set; }
}
public class ProductPrice
{
public int Id { get; set; }
public int Degree{ get; set; }
public int Price{ get; set; }
}
I use this code.but don't ordered
_db.Products.Select(m=>m.ProductPrices.OrderBy(o=>o.Price))
You don't need Select, juste call OrderBy and Max directly
var listSorted = _db.Products.OrderBy(p => p.ProductPrices.Max(pp => pp.Price))
If you want to order products by the maximum product price then do this:
_db.Products.OrderBy(p => p.ProductPrices.Max(pp => pp.Price))
Related
I have this Report model:
public class Report
{
[Key]
public int Id { get; set; }
public string ReporterId { get; set; }
[ForeignKey("ReporterId")]
public virtual ApplicationUser Reporter { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
public ReportType ReportType { get; set; }
}
Now, I want to return a List<Report> ordered by the most repeated element based on ProductId to the least repeated element.
Is there any way on how to do this with linq?
Try this:
var newList = List.GroupBy(p=> p.ProductId )
.OrderByDescending(x=> x.Count())
.SelectMany(x=> x).ToList();
My scenario: Users will be able to create lists and add items to these lists. What I want to do is to find the items in the lists created by the users at most.
Item Entity
public class Item:BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
public decimal DiscountedPrice{ get; set; }
public virtual ICollection<ItemList> ItemLists { get; set; }
}
Item List Entity
public class ItemList:BaseEntity
{
public string Name { get; set; }
public string Description { get; set; }
public int UserId { get; set; }
public ICollection<Item> Items { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
User Entity
public class User:BaseEntity
{
public string Name { get; set; }
public string Surname { get; set; }
public string Gsm { get; set; }
public string Email { get; set; }
public virtual ICollection<ItemList> ItemLists{ get; set; }
}
my DTO
public class TopItemsForUsers
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string ItemId { get; set; }
public string UserId { get; set; }
public int Quantity { get; set; }
}
My Item repository
var query = _context.Items.Include(l => l.ItemLists)
.GroupBy(g => g.ItemLists)
.Select(z => new TopItemsInLists { ItemId = z.Key.ToString(), Quantity = z.Count() })
.OrderByDescending(z => z.Quantity)
.Take(10);
I want to get products that are very present in users' lists
Where am I doing wrong? If anyone has any other suggestions
Try this query. I hope I understand question correctly.
var query =
from u in _context.Users
from il in u.ItemLists
from i in il.Items
group i by new { UserId = u.Id, ItemId = i.Id } into g
select new TopItemsInLists
{
UserId = g.Key.UserId.ToString(),
ItemId = g.Key.ItemId.ToString(),
Quantity = g.Count()
};
query = query
.OrderByDescending(z => z.Quantity)
.Take(10);
Suppose I have an entity:
public class Table {
[Key]
[Column]
public long TableId { get; set; }
[ForeignKey("TableId")]
[JsonIgnore]
[Association("Diners", "TableId", "TableId")]
public List<Diner> DinersAtTable { get; set; }
[NotMapped]
public int Total { get; set; }
}
public class Diner {
[Key]
[Column]
public long DinerId { get; set; }
public long TableId { get; set; }
}
public class Item {
[Key]
[Column]
public long ItemId { get; set; }
public long DinerId { get; set; }
public long Price { get; set; }
}
I want to fill Total with the sum of the Prices of all the Items ordered by the diners, but since I'm not really interested in the Items themselves, I would like to fill the Total in the query which gets me the Table entity.
So, I'd like something like:
var query = ctx.Tables.Include(t => t.DinersAtTable)
.Include(t = t.DinersAtTable.Select(d => d.Include(Items)))
.AssignToUnmapped(t => t.Total = t.Diners.Items.Sum()).Select(t => t.TableId, t.Total)
Obviously AssignToUnmapped and the final Select is what I'm not sure how to achieve. Any idea how I should do that?
i have the following problem, i need access to items of sales line from salesheader, when i try access by entity works fine by lazy loading, but i try map with Automapper 6
canĀ“t access to Item from sales header
thanks
public class SalesHeader
{
public int DocumentNo { get; set; }
public virtual ICollection<PostedSalesLine> SalesLines { get; set; }
}
public class SalesLine
{
public int LineNo { get; set; }
public int DocumentNo { get; set; }
public int ItemId { get; set; }
public virtual Item Item { get; set; }
public int Quantity { get; set; }
public decimal Amount { get; set; }
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public decimal UnitCost { get; set; }
public decimal UnitPrice { get; set; }
}
var result = unitOfWork.SalesHeader.GetById(documenNo);
Mapper.Initialize(cfg => cfg.CreateMap<SalesHeader, SalesHeaderDTO>()
return Mapper.Map<SalesHeaderDTO>(result);
Done!
Dont use lazy loading, it creates a mess of proxys
public IEnumerable<SalesHeader> GetAllFullDocuments()
{
return SalesContext.SalesHeader.Include(sh => sh.SalesLines.Select(i => i.Item))
.Include(sh => sh.SellToCustomer)
.Include(sh => sh.BillToCustomer)
.ToList();
}
I want to create a lambda expression to query a collection by a collection
In a EF code first environment, I have the following data objects
I have a class named price
public class Price
{
public int Id { get; set; }
public string DisplayName { get; set; }
public double Amount { get; set; }
public bool IsActive { get; set; }
public int ItemId { get; set; }
[ForeignKey("ItemId")]
public virtual Item Item { get; set; }
public ICollection<PriceOption> PriceOptions { get; set; }
}
And a related class named
public class PriceOption
{
public int Id { get; set; }
public int PriceId { get; set; }
[ForeignKey("PriceId")]
public virtual Price Price { get; set; }
public int OptionId { get; set; }
[ForeignKey("OptionId")]
public virtual Option Option { get; set; }
}
I have two search criteria
int ItemId
List<int> optionIds
I want to create a lambda expression to select all prices that are equil to the ItemId (easy) and where the PriceOptions
collection contains all of the optionIds.
The Idea is something like this but of course this it just to show what I am trying to achive.
List<Price> prices = _priceRepository.FindAll().Where(x => x.ItemId == item.Id && x.PriceOptions.All(y => y.OptionId == optionIds)).ToList();
Thank you for your help
Earl
The following LINQ query based on Contains and Count methods produces a simpler (thus eventually faster) SQL query:
var matchCount = optionIds.Count;
var prices = _priceRepository.FindAll()
.Where(p => p.ItemId == ItemId &&
p.PriceOptions.Count(po => optionIds.Contains(po.OptionId)) == matchCount)
.ToList();