I have a collection (Child Collection) on which I am performing some operation to make another collection (Parent Collection). But that I do in a dirty foreach
loop. I want to do it in an elegant way.
GroupInfo grpInfo;
List<GroupInfo> lstGroupInfo = new List<GroupInfo>();
foreach (AddressInfo addressInfo in subDetails)
{
if (lstGroupInfo.Where(u => u.Addres1 == addressInfo.Address1).Count() > 0)
{
grpInfo = lstGroupInfo.Where(u => u.Addres1 == addressInfo.Address1).SingleOrDefault();
if (addressInfo.Rural)
grpInfo.Rural = true;
else if (addressInfo.Urban)
grpInfo.Urban = true;
grpInfo.SubDetails.Add(addressInfo);
}
else
{
grpInfo = new GroupInfo();
grpInfo.AddressID = addressInfo.AddressID;
grpInfo.LocationID = addressInfo.NamedLocationID;
if (addressInfo.Rural)
grpInfo.Rural = true;
else if (addressInfo.Urban)
grpInfo.Urban = true;
}
}
GroupInfo
class is:
public class GroupInfo
{
public string Address1
{
get;
set;
}
public int AddressID
{
get;
set;
}
public int? LocationID
{
get;
set;
}
}
I want to do it in LINQ lambda way.