I'm needing some LINQ expert advice. I am having to convert a SQL stored proceedure over to LINQ.
The SQL statement is structured like below:
SELECT SUM((Amt)
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table1.Date < endDate
GROUP BY Table2.Name
How would I group all of the amounts and group them by the name on table2? I have tried numerous ways to do this. Below is one way that I tried to do this:
var query = from Table1 in datatable.AsEnumerable()
join Table2 in Datatable2.AsEnumerable()
on Table1.Field<int>("ID") equals Table2.Field<int>("ID")
where Table1.Field<DateTime>("Date").Date < enddate.Date
group Table2 by new { Name = Table2.Field<string>("Name") } into c
select new
{
Amount = c.Sum(p => p.Field<double>("Amount"))
};
I am able to successfully pull all of the names and group the names, but when it comes to the various amounts and add them together by how they are grouped, I have a really hard time trying to get it to work. The code above says that the column amount doesn't exist in Table2. Would anybody happen to know how to do this successfully? I would highly appreciate any help.