tags:

views:

523

answers:

2

Apologies if this has been asked already, but I couldnt find it.

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

@Ian P - thanks to your answer.

For the finished solution I also needed a bit extra as below...

Old version.

public List<OldClassName> GetSomething()
{
    var query = from names in _context.OldClassNames
                select names;

    return query.ToList();
}

New version with column renaming.

public List<NewClassName> GetSomething()
{
    var query = from names in _context.OldClassNames
                select new NewClassName
                {
                    NewColumName = names.OldColumnName
                };
    return query.ToList();
}
+7  A: 

Use an anonymous type:

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

Good luck!

Ian P
thanks. I just found this question as well.http://stackoverflow.com/questions/128277/linq-custom-column-names
Paul Rowland
A: 

@Ian P - thanks to your answer.

For the finished solution I also needed a bit extra as below...

Old version.

public List<OldClassName> GetSomething()
{
    var query = from names in _context.OldClassNames
                select names;

    return query.ToList();
}

New version with column renaming.

public List<NewClassName> GetSomething()
{
    var query = from names in _context.OldClassNames
                select new NewClassName
                {
                    NewColumName = names.OldColumnName
                };
    return query.ToList();
}
Paul Rowland