tags:

views:

851

answers:

3

In SQL you can selecta constant value:

Select "Constant Text", Column1, Column2 From TableX

and each row returned from TableX starts with a column containing the text "Constant Text".
Any ideas on how I can do this in LINQ to SQL?
If I do the above I get the error message "Range variable name can be inferred only from a simple or qualified name with no arguments."

A: 

from tx in dc.TableX select new { "constant text", tx.Column1, tx.Column2 };

KristoferA - Huagati.com
A: 
var db = new DataContext();

var query = from x in db.TableX
            select new {"Constant Text", x.Column1, x.Column2};

..i think it's something like that.

cottsak
+1  A: 

Actually, each property in the resulting anonymous class needs a name, so in the following code, we are naming the constant column ConstantColumn. The 2nd and 3rd properties will by default take the names of the queried columns, so they will be named Column1 and Column2 respectively:

var query = from x in db.TableX
            select new
            {
                ConstantColumn = "Constant Text",
                x.Column1,
                x.Column2
            };
Lucas