-4

I have an viewmodel that has some relationships built.

public class object()
{
    public int Id  {get; set;}
    public string Name {get;set;}
    public decimal Amount {get;set;}
}

public class myViewModel()
{
    public int Id {get;set;}
    public List<object> myObjects {get;set;}

}

What I would like to do is change the Amount for a specific myObject element given the object name.

var query = new myViewModel();

Assume that I have populated data into the myObjects List.

var record = query.myobjects.FirstOrDefault(x => x.Name = "Test");

Pulls the correct element from this list, but how to update the vale within the list is what I am stuck on. I have tried:

query.myObject.FirstOrDefault(x => x.Name == "Test").Amount = 99;

and

query.myObject.FirstOrDefault(x => x.Name == "Test") == record;

neither works.

gilesrpa
  • 969
  • 1
  • 12
  • 35
  • 6
    `public class object()` is not valid class declaration – Sergey Berezovskiy May 23 '17 at 14:20
  • 2
    Neither is `public class myViewModel()`... – Matt Hogan-Jones May 23 '17 at 14:21
  • 1
    What "doesn't work" with the first one? For the second one, why are you comparing `FirstOrDefault(...)` to `result`? – sab669 May 23 '17 at 14:23
  • basically you create a method UpdateAmount and go stepwise, e.g. – CHS May 23 '17 at 14:24
  • Please show your actual code, as the posted one doesn´t compile at all and thus isn´t adequate to reproduce your issue. – MakePeaceGreatAgain May 23 '17 at 14:26
  • notice in this line you need to use == instad of = : `var record = query.myobjects.FirstOrDefault(x => x.Name = "Test");` Like `var record = query.myobjects.FirstOrDefault(x => x.Name == "Test");` – Timon Post May 23 '17 at 14:27
  • I can't post the answer I typed up as its been marked as duplicate but I did create a dotnetfiddle: https://dotnetfiddle.net/2focv8 Using code similar to what you said you tried first, I am successfully able to update the object's property. – sab669 May 23 '17 at 14:38

2 Answers2

1

Here's the complete code I ran in LinqPad:

void Main()
{
    var query = new myViewModel();
    query.myObjects.FirstOrDefault(x => x.Name == "Test").Amount = 99;  
    query.Dump();
}

// Define other methods and classes here
public class objectX
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Amount { get; set; }
}

public class myViewModel
{

    public int Id { get; set; }
    public List<objectX> myObjects { get; set; }

    public myViewModel()
    {
        myObjects = new List<UserQuery.objectX>();
        myObjects.Add(new objectX { Amount = 100, Id = 1, Name = "ABC" });
        myObjects.Add(new objectX { Amount = 200, Id = 2, Name = "Test" });
        myObjects.Add(new objectX { Amount = 300, Id = 3, Name = "GHI" });
    }

}

output:

List (3 items) Id | Name | Amount

1 | ABC | 100

2 | Test | 99

3 | GHI | 300

| | 499

James Curran
  • 101,701
  • 37
  • 181
  • 258
0

'You question shows you should go & read a good C# book...

basically you are confusing = and ==. What you could do is create a method and proceed step-wise, e.g.

public class myViewModel
{
    public int Id {get;set;}
    public List<object> myObjects {get;set;}

    public bool ChangeAmountByName(string name, decimal amount)
    {
        var match = this.myobjects.FirstOrDefault(x => x.Name == name);
        if (match == null) return false;

        match.Amount = amount;
        return true;
    }
}

This is just one of several possible solutions. But it is always important to "divide and conquer", i.e. split your task into steps: Here it is: First get your matching record. Verify it is there; react somehow it is missing. Then do your value update!

CHS
  • 138
  • 1
  • 1
  • 7