I have a linq to sql query that produces duplicates. i can seem to figure out how to eliminate these duplicate records. Maybe someone on here can help. The idea is to retrieve the sum of the “total” from table2 that occurs for dates in table 1 that are greater than dates of table2 and also have dates in table2 that are greater than 2011-01-01. What i have achieves this but because of duplicates i get exactly double the sum. Your help is much appreciated
Table structure:
table1: id, date
table2: id, date, total
The query
from t1 in table1
join t2 in table2 on t1.id equals t2.id into x
from y in x
where t1.id == "123" && t1.date > y.date && y.date > DateTime.Parse("2011-01-01")
group t1 by new { y.total} into g
select g.Sum(t1 => t1.total);
You should split the query in multiple steps:
Select the data you want:
Call distinct to avoid repeating items:
Get the sum: