0

I am using this line to return a value, but instead its getting a True

var valor = valoresCampoAdicionalesEmpresa.Select(p => p.Key == propiedad.Nombre).First();

This the view data passes from the controller

var valoresCampoAdicionalesEmpresa = (Dictionary)ViewData["ValoresCampoAdicionalesEmpresa"];

public ActionResult Edit(int? id)
        {
            var listFields = from b in db.Propiedades
                             where b.Entidad.Nombre == "Empresa"
                             select b;
            ViewData["CamposAdicionalesEmpresa"] = listFields.ToList<Propiedad>();

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Empresa empresa = db.Empresas.Find(id);
            if (empresa.PropiedadesExtra != null)
            {
                XElement xmlTree = XElement.Parse(empresa.PropiedadesExtra);
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (var el in xmlTree.Elements())
                {
                    dict.Add(el.Name.LocalName, el.Value);
                }

                ViewData["ValoresCampoAdicionalesEmpresa"] = dict;
            }

            if (empresa == null)
            {
                return HttpNotFound();
            }
            return View(empresa);
        }

4 Answers 4

4

In that case, FirstOrDefault is what you want:

var valor = valoresCampoAdicionalesEmpresa.FirstOrDefault(p => p.Key == propiedad.Nombre);
Sign up to request clarification or add additional context in comments.

Comments

3

Change your Select to be a Where. Where uses a predicate to filter the data and return the same structure...just a subset. Select on the other hand changes the data structure to be whatever is evaluated in the provided function. In your case you are changing the structure to be a single Boolean result equivalent to whether Key equals Nombre. The fixed code is below:

var valor = valoresCampoAdicionalesEmpresa.Where(p => p.Key == propiedad.Nombre).First();

And, as pointed out in the comments, First* accepts a predicate itself, so you can simply call First as a combined Where and First

var valor = valoresCampoAdicionalesEmpresa.First(p => p.Key == propiedad.Nombre);

First will throw if there is nothing, so you can use FirstOrDefault if that fits better

2 Comments

Correct but First takes the same condition of Where
@Steve Agreed, amended!
2

Needs to be .Where(), not .Select(). .Select() returns the result of the expression, which is a boolean. .Where() returns the result where the expression is true.

Comments

1

use .FirstOrDefault() and wouldnt hurt to be a bit more explicit in the declaring.

string? valor = valoresCampoAdicionalesEmpresa.FirstOrDefault(p => p.Key == propiedad.Nombre);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.