1

I need to query multiple columns using Linq query and as far as I know I need to use the Curly braces '{}'

I wrote the following query but for some strange reason its printing the opening and closing braces '{}' that's in the statement.

var q = db.table1.Where(n => n.user== USER).Select(n => new { n.Name, n.LastName }); 
            ViewBag.test = q;

In My View

@foreach (var item in ViewBag.test)
{
    @item
}

Output:

{ Name = john, LastName=Smith}
{ Name = Mike, LastName=mojie}

Why is it printing the curly braces?!

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
ceci
  • 429
  • 1
  • 7
  • 22

1 Answers1

4

Because that's the default implementation of ToString for an anonymous type.

You could try:

@(item.Name + " " + item.LastName)
Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • I tried it and it return this error: `'object' does not contain a definition for 'Name'` – ceci Sep 29 '14 at 20:52
  • @ceci: I think that's because `ViewBag` is dynamic. If you use `dynamic` instead of `var` it may work. You could also try actually declaring a type and using that instead of an anonymous type. – Andrew Whitaker Sep 30 '14 at 01:52