Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
I have List<Products> Data,
ProductName	 ProductPrice 	    DateTime
Prod1	           66          22/10/2013
Prod2	           55	       21/03/2014
.	                .          	.
.	                .	        .
.	                .	        .

Now I divided this list into two more resultant list by grouping,
Var list1=Products.GroupBy(p => p.DateTime.Value.Hour);
Hours	Counts
1	   23
2	   66
.	   .
.	   .
.          .

Var list2=Product. .Where(p=>p.productprice <= 50).GroupBy(p => p.DateTime.Value.Hour);
Hours	Counts
1	     20
2	     55
.	     .
.	     .
.	     .


Now I want to divide list2 counts by list1 counts one by one and store it in third list as result,  ("The below is the resultant list which i want")

Hours	Counts
1	    20 / 23
2	    55 / 66
.	    .
.	    .
.	    .


how can I do this or is there any other simple way of doing it in Linq To Entity.
Posted
Updated 30-Sep-14 20:12pm
v2

You can do like this .

Var list1=Products.GroupBy(p => p.DateTime.Value.Hour);
Var list2=Product. .Where(p=>p.productprice <= 50).GroupBy(p => p.DateTime.Value.Hour);

var List3 = (from ObjLst1 in list1
              join ObjLst2 in list2
             on ObjLst1.Hours equals ObjLst2.Hours
             where ObjList2.Counts > 0
             select new {
                         Hours = ObjLst1.Hours,
                         Counts = ObjLst1.Counts/ObjList2.Counts
                        }).ToList();
   
Comments
shaprpuff 1-Oct-14 3:17am
   
It works and i get the resultant list Thanks "Sudhansu Sekhar Prusty".
just for advance can it be done in one query only.
you can do this in one query in a similar fashion.


C#
var Lst3 = (from Obj2 in Products.Where(p=>p.productprice <= 50).GroupBy(P => P.DateTime.Value.Hour).Select(g => new {Hours = g.Key, Counts = g.Count()})
join Obj in Products.GroupBy(P => P.DateTime.Value.Hour).Select(g => new {Hours = g.Key, Counts = g.Count()})
on Obj.Hours equals Obj2.Hours
where Obj.Hours > 0
select new {
Hours = Obj.Hours,
Counts = Obj.Counts/Obj2.Counts
            }).ToList();
   
v2

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