-1

Table data is:

Info
----------------------------------------
num    name   age    expenseN    cost    group
1      a      20
2      b      21 

InfoDetail
----------------------------------------
num    expenseN    cost       group
1      001         10.00      x
2      001         20.00      x
3      002         20.00      x
4      003         30.00      y

This is code:

public class _infoRepository
{
    public string name { get; set; }
    public int age { get; set; }
    public string expenseN { get; set; }
    public decimal cost { get; set; }
    public string group { get; set; }
}
public class _infoDetailRepository
{
    public string expenseN { get; set; }
    public decimal cost { get; set; }
    public string group { get; set; }
}


List<Info> result = new List<Info>();
var info = _infoRepository.Query(p => p.name = "a").FirstOrDefault();
var listInfoDetail = _infoDetailRepository.Query(p => p.group == "x").ToList();
for (int i = 0; i < listInfoDetail.Count; i++)
{
    result.Add(new Info()
    {
        name = info.name,
        age = info.age,
        expenseN = listInfoDetail[i].expenseN,
        cost = listInfoDetail[i].cost,
        group = listInfoDetail[i].group
    });
}
return result;

After running this code, the result of the result variable is as follows:

result
--------------------------------------------------
num    name   age    expenseN    cost      group
1      a      20     001         10.00     x
2      a      20     001         20.00     x
3      a      20     002         20.00     x

However, that was not the result I wanted, the result I expected was like this:

result
--------------------------------------------------
num    name   age    expenseN    cost      group
1      a      20     001         30.00     x
2      a      20     002         20.00     x

After all, i want to group by and sum the result variable to give me the desired result. Someone please help me in this situation, thanks

Triet Pham
  • 88
  • 1
  • 14

2 Answers2

1

the listInfoDetail Must be grouped

Instead of

var listInfoDetail = _infoDetailRepository.Query(p => p.group == 
                     "x").ToList();

Use

 var listInfoDetail =_infoDetailRepository.Query(p => p.group =="x")
    .ToList().GroupBy(P=>new {P.expenseN, P.group})
    .Select(P=>new 
         {P.Key.expenseN, P.Key.group, cost=P.Sum(p=>p.cost)}
    );
Peter Isaac
  • 352
  • 2
  • 12
0

Update your for loop like:

for (int i = 0; i < listInfoDetail.Count; i++)
        {
            if (result.Count(j => j.expenseN == listInfoDetail[i].expenseN) == 0)
            {
                result.Add(new Info
                {
                    name = info.name,
                    age = info.age,
                    expenseN = listInfoDetail[i].expenseN,
                    cost = listInfoDetail.Where(j => j.expenseN == listInfoDetail[i].expenseN).Sum(j => j.cost),
                    group = listInfoDetail[i].group
                });
            }
        }
Praneet Nadkar
  • 823
  • 7
  • 16