Format query in linq to send to the ViewBag

0

I made a query in linq, but the result was not expected, it seems the problem is simple but I can not solve it.

Result on label:

  

{Year = 2016/2017}

Expected result on label:

  

2016/2017

Controller:

//query
     var queryAnoPastoral = (from a in db.AnoPastoral
                                        orderby a.AnoPastoralID descending
                                        select new { a.Ano }).First();

       ViewBag.AnoPastoral = queryAnoPastoral.ToString();

View:

@Html.Label((string)@ViewBag.AnoPastoral, htmlAttributes: new { @class = "control-label col-md-2" })
    
asked by anonymous 10.03.2016 / 20:44

1 answer

4

Do you want to return only the value of the Year right? The way you did you returned an anonymous object so when you use .toString() it comes with the keys, you can change your query to return the direct year that will work the way you expect it to.

var queryAnoPastoral = (from a in db.AnoPastoral
                        orderby a.AnoPastoralID descending
                        select a.Ano).First();

ViewBag.AnoPastoral = queryAnoPastoral.ToString();
    
10.03.2016 / 21:13