Search code examples
.netlinq

How do I do this SELECT statement that uses column renaming (or column alias) with LINQ?


How do I get 'anotherColumnName' in LINQ?

SELECT thisColumnName as anotherColumnName FROM TableName

I have the following which obviously gives me 'thisColumnName' not 'anotherColumnName'

    var query = from names in _context.TableName
                select names;
    return query.ToList();

Solution

  • Use an anonymous type:

    var query = (from name in _context.Table
                select new { anotherColumnName = name.ColumnName });