what I am trying to achieve here is that I want from that LINQ query to return the list with two poperties: billNo, and number of occurences of the importcode on the same fromDate.
So here we have a billNo 1 and 2 both have the same importcode which appears in two rows on the same date (01/01/2020) thus count is 2.
If it helps to clarify, think of it as import code should only cover one distinct fromDate. If it appears multiple time, I would like to see how many times (count) and which BillNo s are like that.
So expected result for dataset below would be:
| BillNo | Count |
|---|---|
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
I struggle to figure out how to select BillNo if it is not used for grouping.
Thanks a lot for helping.
var rows = new List<ImportRow>()
{
new ImportRow {billNo= 1, importCode = "one", fromDate = new DateTime(2020, 1, 1)},
new ImportRow {billNo= 2, importCode = "one", fromDate = new DateTime(2020, 1, 1)},
new ImportRow {billNo= 3, importCode = "two", fromDate = new DateTime(2020, 1, 1)},
new ImportRow {billNo= 4, importCode = "two", fromDate = new DateTime(2020, 2, 1)},
new ImportRow {billNo= 5, importCode = "one", fromDate = new DateTime(2020, 3, 1)}
};
public class ImportRow : IEnumerable
{
public int billNo { get; set; }
public string importCode { get; set; }
public DateTime fromDate { get; set; }
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
var billNosWithCounts = rows.GroupBy(info => new { info.importCode,
info.fromDate })
.Select(group => new
{
FromDate = group.Key.fromDate,
Count = group.Count()
});