Click here to Skip to main content
15,891,726 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi,
I have list like List<promoptionproducts>.This list contains the objects.

PromoptionProducts


Public Class PromotionProducts
{
Public long int

Public long ProductId{get;set;}

public long qty{get;set;}

public decimal price{get;set;}

Public int max {get;set;}
}
Case1:

C#
<pre>List<PromoptionProducts> promotionproduct=new List<PromoptionProducts>(new promotionproduct({Id=1,productId=101,qty=4,price=25,max=3 });


I want to get the qty of products from list based on max that means I want 3qtyproducts from the list based on 3.

Case2:

C#
<pre>List<PromoptionProducts> promotionproduct=new List<PromoptionProducts>(new promotionproduct({Id=1,productId=101,qty=2,price=25,max=3 },new promotionproduct({Id=2,productId=102,qty=2,price=35,max=3 });


I want to get the qty of products from list based on max that means I want 3 qty products from the list based on 3.

While using linq quries.


Please help me.
Thank you.

What I have tried:

I want to get the qty of products from list based on max that means I want 3 qty products from the list based on 3.


I want to get the qty of products from list based on max that means I want 3qtyproducts from the list  based on 3.
Posted
Updated 20-Oct-19 20:12pm

1 solution

You can do something like below I had to change some of your code


C#
public class PromotionProducts
{
	public long Id { get; set; }
	public long ProductId { get; set; }
	public long qty { get; set; }
	public decimal price { get; set; }
	public int max { get; set; }
}


C#
var promotionproduct = new List<PromotionProducts>() { new PromotionProducts() { Id = 1, ProductId = 101, qty = 4, price = 25, max = 3 } };
			var promotionproduct2 = new List<PromotionProducts>() { new PromotionProducts() { Id = 1, ProductId = 101, qty = 2, price = 25, max = 3 }, new PromotionProducts() { Id = 2, ProductId = 102, qty = 2, price = 35, max = 3 } };



promotionproduct = promotionproduct.Where(x => x.max == 3).ToList();
var qty = promotionproduct.Where(x => x.max == 3).Select(x => x.qty).ToList();

promotionproduct2 = (from p in promotionproduct2
		     where p.max > 2 || p.max == 3
			 select p).ToList();


in the where clause you can change the conditions according to your needs
 
Share this answer
 
Comments
Ramesh p 21-Oct-19 2:23am    
Actually product quantity grater than max.I want to get the max products that means products have 4 quantity products but i need 3 quantity products only but you filter the max 3 objects from the list
dnxit 21-Oct-19 2:56am    
Yeah you can change the where clause, I gave you an idea. Something like this

promotionproduct = promotionproduct.Where(x => x.qty == 3 || x.qty < x.max).ToList();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900