0

Im trying to add a constant to a LINQ query in BLtoolkit.

var query = dbManager.Table.Select(x=>new { x.column, cnst = 1 });

but in the result there is only 'column' column, but no 'cnst' column.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
John Smith
  • 1,783
  • 6
  • 22
  • 36

2 Answers2

1

This should also work:

var query = dbManager.Table.Select(x=>new { column = x.column, cnst = 1 });
Hogan
  • 69,564
  • 10
  • 76
  • 117
0

Try this form instead:

var query = from x in dbManager.Table
        select new
        {
            x.column,
            cnst = 1
        };
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • How/why is this different? I always assumed both statements to be interchangeable. – Flater Feb 24 '14 at 15:54
  • I'm not sure. But all of the code examples that I've seen that use a lambda expression also use an *actual* type to Select into, not an anonymous type. – Robert Harvey Feb 24 '14 at 16:01
  • I've been able to do it with anonymous types. In MVC2 I used an overridable version of `.Select(x => new { Text = x.a, Value = x.b })` to get a nice and clean approach to casting entities to dropdownlists. I generally like using lambdas over using the other way (I have no word for it except "SQL like"), but that's a preference of layout. – Flater Feb 24 '14 at 16:13
  • @Flater: In your approach, the lambda variable is always being referenced. – Robert Harvey Feb 24 '14 at 16:13
  • It also used variables that didn't use the lambda variable, but an external (from outside the lambda) variable. Those work as well. But maybe purely const values don't. – Flater Feb 24 '14 at 16:15