Reputation: 6868
This is my Table:
PupilNutrition
Id PupilId NutritionId
1 10 100
2 10 101
My another table Nutrition:
Id Nutritioncategory BatchId NutritionRate NutritionId Operation
1 A 1 9000 100 1
2 B 1 5000 100 0
3 C 1 5000 100 1
4 D 2 6000 101 2
5 E 2 7000 101 2
6 F 2 8000 101 0
This is one field to store final Rate:
decimal Rate= 0;
Case 1: Operation field with Value 0 and Batch Id 1
Rate= Rate + NutritionRate(i.e 5000 because for batch id 1 with condition 0 only 1 record is there).
Case 2:Now for operation field with Value 1 and Batch Id 1
Here i want to sum up both the value i.e(10000 and 5000) but during addition if sum of both is greater than 10000 then just take 10000 else take sum like this:
if(9000 + 5000 > 10000)
Rate= 10000
else
Rate=Rate + (Addition of both if less than 10000).
Case 3:Now for operation field with Value 0 and Batch Id 2
Rate= Rate + NutritionRate(i.e 8000 because for batch id 1 with condition 0 only 1 record is there).
Case 4:Now for operation field with Value 2 and Batch Id 2
Here I will select Maximum value from Nutrition Rate field if there are 2 records:
Rate=Rate - (Maximum value from 6000 and 7000 so will take 7000).
My Class file:
public partial class PupilNutrition
{
public int Id { get; set; }
public int PupilId { get; set; }
public int NutritionId { get; set; }
}
public partial class Nutrition
{
public int Id { get; set; }
public string Nutritioncategory { get; set; }
public decimal NutritionRate { get; set; }
public Nullable<int> NutritionId { get; set; }
}
This is how i have Done:
batchId=1 or 2;//
List<int> NutritionId = db.PupilNutrition.Where(x => PupilId == 10).Select(c => c.NutritionId).ToList(); //output 100,101
var data = db.Nutrition.Where(x => x.BatchId == batchId && NutritionId.Contains(x.NutritionId.Value)).ToList();
I have tried like this but its a labour process:
var data1=data.where(t=>t.Operation==0);
Rate= Rate + data1.Sum(p => p.NutritionRate);
Similarly for Operation 1:
var data2=data.where(t=>t.Operation==1);
This is left because here i need to sum up 2 value if two records are there as shown in my above cases and if sum is greater than 10000 than select 10000 else select sum.
Similarly for Operation 2:
var data3=data.where(t=>t.Operation==2);
Rate= Rate - data3.Max(p => p.NutritionRate);
I think i have done a labour process as this can be done even in better way with linq query only i guess.
So can anybody help me to simplify this whole process in linq query or even some better way and provide solution for Operation Value 2 which is left?
Upvotes: 19
Views: 803
Reputation: 7017
Shortest is not always the best.
Personally, I think that single complex LINQ statement can be hard to read and hard to debug.
Here is what I would do:
public decimal CalculateRate(int pupilId, int batchId)
{
return db.Nutrition
.Where(x => x.BatchId == batchId && db.PupilNutrition.Any(y => y.PupilId == pupilId && y.NutritionId == x.NutritionId))
.GroupBy(x => x.Operation)
.Select(CalculateRateForOperationGroup)
.Sum();
}
public decimal CalculateRateForOperationGroup(IGrouping<int, Nutrition> group)
{
switch (group.Key)
{
case 0:
// Rate = Sum(x)
return group.Sum(x => x.NutritionRate);
case 1:
// Rate = Min(10000, Sum(x))
return Math.Min(10000, group.Sum(x => x.NutritionRate));
case 2:
// Rate = -Max(x)
return -group.Max(x => x.NutritionRate);
default:
throw new ArgumentException("operation");
}
}
Then you could use it as follows:
var rateForFirstBatch = CalculateRate(10, 1);
var rateForSecondBatch = CalculateRate(10, 2);
Upvotes: 11
Reputation: 995
Easier with a group by:
int sumMax = 10000;
var group = from n in nutrition
group n by new { n.Operation, n.BatchId } into g
select new { BatchId = g.Key.BatchId,
Operation = g.Key.Operation,
RateSum = g.ToList().Sum(nn => nn.NutritionRate),
RateSumMax = g.ToList().Sum(nn => nn.NutritionRate) > sumMax ? sumMax : g.ToList().Sum(nn => nn.NutritionRate),
RateMax = g.ToList().Max(nn => nn.NutritionRate) };
var result = from g in group
select new {
BatchId = g.BatchId,
Operation = g.Operation,
Rate = g.Operation == 1 ? g.RateSumMax :
g.Operation == 2 ? g.RateMax :
g.RateSum //default is operation 0
};
var totalRate = result.Sum(g=>g.Rate);
Upvotes: 5
Reputation: 7028
try this. I broke the query to make it clear but you can combine this in one statement only.
//// Join the collection to get the BatchId
var query = nutritionColl.Join(pupilNutrition, n => n.NutritionId, p => p.NutritionId, (n, p) => new { BatchId = p.Id, Nutrition = n });
//// Group by BatchId, Operation
var query1 = query.GroupBy(i => new { i.BatchId, i.Nutrition.Operation });
//// Execute Used cases by BatchId, Operation
var query2 = query1.Select(grp =>
{
decimal rate = decimal.MinValue;
if (grp.Count() == 1)
{
rate = grp.First().Nutrition.NutritionRate;
}
else if (grp.First().BatchId == 1)
{
var sum = grp.Sum(i => i.Nutrition.NutritionRate);
rate = sum > 10000 ? 10000 : sum;
}
else
{
rate = grp.Max(i => i.Nutrition.NutritionRate);
}
return new { BatchId = grp.First().BatchId, Operation = grp.First().Nutrition.Operation, Rate = rate };
}
);
//// try out put
foreach (var item in query2)
{
Console.WriteLine("{0} {1} {2} ", item.Operation, item.BatchId, item.Rate);
}
hope it helps
Upvotes: 4
Reputation: 27357
Update This query looks like it should get the job done for you. Note, I haven't tested it, so there may be a syntax error or two, but the idea seems sound.
var rate = db.Nutrition
.GroupBy(n => new { n.BatchId, n.Operation })
.Select(g => g.Sum(n => n.NutritionRate) > 10000 ? 10000 : g.Sum(n => n.NutritionRate))
.Sum();
Or, if you want queries for each 'case':
rate = db.Nutrition
.Where(n => n.BatchId == 1 && n.Operation == 0)
.Sum(n => n.NutritionRate); //Case 1
rate += Math.Min(
10000, //Either return 10,000, or the calculation if it's less than 10,000
db.Nutrition
.Where(n => n.BatchId == 1 &&
n.Operation == 1) //filter based on batch and operation
.Sum(n => n.NutritionRate) //take the sum of the nutrition rates for that filter
); //Case 2
rate += db.Nutrition
.Where(n => n.BatchId == 2 && n.Operation == 0)
.Sum(n => n.NutritionRate); //Case 3
rate += Math.Min(
10000, //Either return 10,000, or the calculation if it's less than 10,000
db.Nutrition
.Where(n => n.BatchId == 2 &&
n.Operation == 2) //filter based on batch and operation
.Sum(n => n.NutritionRate) //take the sum of the nutrition rates for that filter
); //Case 4
Upvotes: 4