It cannot be assigned as it is read-only after doing a "Join"

Asked

Viewed 499 times

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 that, I need to use a foreach to modify. I cannot modify valor1, because it says it can’t be assigned, it’s read-only.

How can I modify value?

  • It’s not enough to make a valor1 = string.Format("{0:N}",post), nay?

  • @Felipeavelar, I already tried this tbm, I get error: LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression.

  • So, I believe that making a select after Where is best.

  • @Felipeavelar, I get the same mistake, there’s a way I get this thing out of "read-only" ?

  • It’s read-only and you really can’t assign, what do you want? Display in a View???

  • That’s right Virgil, I intend to display in the view.

  • @Matheusmiranda posts her View your Controller and the responsible method so that I can propose something to you! do not need to format like this.

  • @Virgilionovic edited post. In the database is recorded as 100000,01 say user typed in 100.000,01. How can I get the line that contains100000,01 ?

  • are you filtering by currency value? the user on the screen typed in the Brazilian standard and the Bank is in different format? is that it??? if you need to filter in the database by the data sent from the screen?

  • @Virgilionovic as in !!!

  • @Matheusmiranda I believe you have to save the value in the database being Numeric even (decimal house with . in sql server, for example). If the user type 100,000.01, you would have to remove the dots and then convert to decimal again by formatting the number. But this has to be before passing the value to Ingl, because if using format it will give way even as it will try to convert the method into a valid sql.

Show 6 more comments

1 answer

1


It only 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 seems that it has problems because it has two filters that I ended up joining because of its answer and the join perhaps needlessly.

When the expression of Linq (example: ToList(), FirstOrDefault(), etc.), can then work the information on the part of objects, ie, Linq to Object, but, take care that your performance is not compromised, for small volumes does not have so many performance problems, only even for large volumes, ie, will depend on the amount 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'));
  • @Matheusmiranda did not understand well, but, if you are talking about formatting?

  • 1

    Whether you can select after ToList().Select(), for example! , because there you can, because there is object already! what one can not do when it is in SQL ai that can not and is ReadOnly!

  • I wouldn’t indicate that other way there !!! @Matheusmiranda

  • @Matheusmiranda ready I edited by putting a simple text we will delete these comments?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.