Friday, June 11, 2010

Left Outer Join and Group By In LINQ


List Table1 = new List();
List Table2 = new List();

var objList = from ListData in
(from a in Table1
join b in Table2 on a.Id equals b.Id into t2
from t2Data in t2.DefaultIfEmpty()
select new
{
Product = a.Product,
Title = t2Data.Title,
Amount = a.Amount
}
)
group ListData by new
{
ListData.Product,
ListData.Title
} into GroupData
select new
{
Product = GroupData.Key.Product,
Title = GroupData.Key.Title,
Amount = GroupData.Sum(OL => OL.Amount)
};

2 comments:

prasobhkrishnan said...

Thanks.
The grouping and the sum is worked properly.
Nice example.
Keep going.
PPKN

prasobhkrishnan said...

Thanks,
The code for grouping with LINQ is worked properly.
Keep going.
PPKN