Can not be assigned because it is read-only after doing a "Join"

1

Controller:

[HttpPost]
public ActionResult Listar_Json()
{
  var id = 5;
  var search = Request["search[value]"]; //Aqui pega valor digitado no input, digamos que ele digitou 100.000,01

  var query = database.Posts.Join(database.Post_Metas, 
           post => post.ID, 
           meta => meta.Post_ID, 
           (post, meta) => new 
           { 
              valor1 = post.Money1, //100000,01
              valor2 = meta.Money2  //100000,02
           })
        .Where(x => x.Post.ID == id)
        .ToList();

foreach (var item in query )
{
   item.valor1 = item.valor1 ????  //100.000,01
}

query = query
    .Where(x => (x.Money1.ToString() ?? "").Contains(search)
    .ToList();

   return Json(new { data = query }, JsonRequestBehavior.AllowGet);
}

View:

<table id="data_grid" class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>Valor1</th>
            <th>Valor2</th>
        </tr>
    </thead>
</table>

I want to return value 100000,01 for 100,000,01 . For this, I need to use a foreach to modify. I can not change valor1 , because it says it can not be assigned, it's read-only.

How can I change the value?

    
asked by anonymous 02.07.2017 / 20:51

1 answer

1

It just converts the value that comes from the user to the equal value of the bank with Convert.ToDecimal for example:

[HttpPost]
public ActionResult Listar_Json()
{
    var id = 5; 
    var search = Request["search[value]"];     
    decimal value = Convert.ToDecimal(search.Replace(".",""));

    var query = database.Posts.Join(database.Post_Metas, 
           post => post.ID, 
           meta => meta.Post_ID, 
           (post, meta) => new 
           { 
              post, meta
           })
        .Where(x => x.post.ID == id && x => x.post.Money1 == value)
        .ToList();


   return Json(new { data = query }, JsonRequestBehavior.AllowGet);
}

Although it looks like it has problems because there are two filters that I ended up linking because of your answer and maybe join unnecessarily.

When the expression of Linq ( example> ToList() , FirstOrDefault() , etc ) is already materialized, work with the information in the part of objects, that is, Linq to Object , but, be careful that its performance is not compromised, for small volumes does not have so many performance problems, even for large volumes, ie, it depends on the number of rows returned, but, is a way to already send the information with its own characteristics.

Example:

var query = database.Posts.Join(database.Post_Metas, 
           post => post.ID, 
           meta => meta.Post_ID, 
           (post, meta) => new 
           { 
              post, meta
           })
        .Where(x => x.post.ID == id && x => x.post.Money1 == value)
        .ToList()
        .Select(x => x.Money1.ToString('N2'));
    
03.07.2017 / 16:59