hi RB,
I have solved it like this ,
Here are the detail steps how i solved
I think you are using NorthWind database, first I have queried the results to get ContactName and his products ordered using below query
NorthwindEntities ctx = new NorthwindEntities();
List<MyClass> result = (from o in ctx.Order_Details
join p in ctx.Products on
o.ProductID equals p.ProductID
join oo in ctx.Orders on
o.OrderID equals oo.OrderID
join c in ctx.Customers on
oo.CustomerID equals c.CustomerID
select new MyClass {CustName=c.ContactName, ProdName =p.ProductName }).ToList();
I have created another class to store the results
public class MyClass
{
public string CustName { get; set; }
public string ProdName { get; set; }
}
Initialize the class
List<MyClass> finalResult = new List<MyClass>();
Now iterate throught the results fetched and compare ,
foreach (var s in result)
{
MyClass mc = null;
if (finalResult.Count > 0)
{
mc = finalResult.Where(i => i.CustName == s.CustName).FirstOrDefault();
if (mc != null)
{
mc.ProdName += "," + s.ProdName;
}
else
{
mc = s;
}
}
else
{
mc = s;
}
finalResult.Add(mc);
}
GridView1.DataSource = finalResult;
GridView1.DataBind();
This is working fine for me,
Try and let me know
Regards