Hi,
I am new to linq and lamda expressions.
I have a set of class containing lists. I want to update the specific data as per the ids and labels.
For a reference, i am sending the classes.
public class Global
{
public string GlobalId { get; set; }
public string GlobalName { get; set; }
public IList Areas { get; set; }
}
public class Area
{
public string AreaId { get; set; }
public string AreaNam { get; set; }
public IList
}
public class Plot
{
public string PlotId { get; set; }
public string PlotName { get; set; }
}
public class Deal
{
public Global GetDetails()
{
Plot p1 = new Plot();
p1.PlotId = "1";
p1.PlotName = "P1";
Plot p2 = new Plot();
p2.PlotId = "2";
p2.PlotName = "p2";
Area area1 = new Area();
area1.AreaId = "1";
area1.AreaNam = "A1";
Area area2 = new Area();
area2.AreaId = "2";
area2.AreaNam = "A2";
IList
plots.Add(p1);
plots.Add(p2);
area1.Plots = plots;
area2.Plots = plots;
IList areas = new List();
areas.Add(area1);
areas.Add(area2);
Global global = new Global();
global.GlobalId = "1";
global.Areas = areas;
return global;
}
public Global UpdateDeal(string areaId, string plotId, Global globalList, string dealLavel, string nameToBeUpdated)
{
Global globals = globalList;
if (dealLavel == "plot")
{
}
if (dealLavel == "area")
{
}
return globals;
}
}
static void Main(string[] args)
{
Deal deal = new Deal();
Global globalData = deal.GetDetails();
// UpdateDeal(areaId, plotId, listOfGlobaldata, dealLavel, plotname)
Global updatedGlobals = deal.UpdateDeal("1", "2", globalData, "plot", "samplePlot");
}
Now, I want to update as per the below call.
Global updatedGlobals = deal.UpdateDeal("1", "2", globalData, "plot", "newPlotName");
here, I have to update PlotName having areaId = "1" and plotId = "2" with a new PlotName "samplePlot".
and return the updated list.
Can any linq or lamda expression expert help me in this ??
Thanks a lot.
Blocked AccountPosted Mar 11, 2013, 12:52 AM
Just update your UpdateDeal Method
public Global1 UpdateDeal(string areaId, string plotId, Global1 globalList, string dealLavel, string nameToBeUpdated)
{
Global1 globals = globalList;
if (dealLavel == "plot")
{
(from areas in globals.Areas
where areas.AreaId == areaId
from plots in areas.Plots
where plots.PlotId == plotId
select plots).ToList().ForEach((pl) =>
{ pl.PlotName = nameToBeUpdated; }
);
}
if (dealLavel == "area")
{
(from areas in globals.Areas
where areas.AreaId == areaId
select areas).ToList().ForEach((pl) =>
{ pl.AreaNam = nameToBeUpdated; }
);
}
return globals;
}
Jignesh TrivediPosted Mar 11, 2013, 12:32 AM
as per my knowledge, it can be achive by simple foreach loop. no need write complex LINQ
public Global UpdateDeal(string areaId, string plotId, Global globalList, string dealLavel, string nameToBeUpdated)
{
Global globals = globalList;
if (dealLavel == "plot")
{
foreach(var a in globals.Areas)
{
foreach var p in a.Plots)
{
p.PlotName = nameToBeUpdated;
}
}
}
if (dealLavel == "area")
{
foreach(var a in globals.Areas)
{
a.AreaNam = nameToBeUpdated;
}
}
return globals;
}
}
hope this will help you.