2
Answers

Updating a complex (for me :-() list from linq query

Sumitra Paul

Sumitra Paul

11y
1.3k
1

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<Area> Areas { get; set; }
    }

    public class Area
    {
        public string AreaId { get; set; }
        public string AreaNam { get; set; }
        public IList<Plot> Plots { get; set; }
    }

    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<Plot> plots = new List<Plot>();
            plots.Add(p1);
            plots.Add(p2);

            area1.Plots = plots;
            area2.Plots = plots;

            IList<Area> areas = new List<Area>();
            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.

Answers (2)