How alias column name in LINQ?

R

Ronald S. Cook

Hi..

Does anyone know how to alias a column name in LINQ. I.e. like putting "AS
DiagnosisType" after a column in a select statement in SQL?

Return From d In dc.Diagnosis _
Where d.Lookup.LookupCode <> "INA" _
Select d.DiagnosisId, _
d.DiagnosisName, _
d.DiagnosisCode, _
d.Lookup1.LookupValue _ <<<--- Here is where I want "AS DiagnosisType"
Order By DiagnosisName

Thanks,
Ron
 
M

Marc Gravell

Well, you could try aliasing via a projection? i.e. in your LINQ, doing
something like:
select new {
p.MyCharColumn, // default name
Bar = p.Foo // aliased
};

However! The aliases that it uses in the SQL are largely an implementation
detail. As far as I can see, it would be perfectly reasonable for the LINQ
provider to use "c0", "c1", "c2" etc for the columns: as long as it can map
them back to the members in your object model it is none of our business how
it handles it (encapsulation).

IIRC, from inspection I believe LINQ-to-SQL tends to honour aliases, but I
doubt it is guaranteed; what DbLinq does is anyones guess.

Marc
 
M

Marc Gravell

don't ask me how to map that to VB; 'tis a C# forum... as an outside guess:

Select d.DiagnosisId,
d.DiagnosisName,
d.DiagnosisCode,
DiagnosisType = d.Lookup1.LookupValue

also - ignore my DbLinq comment; I thought (incorrectly) that this was part
of a chain involving DbLinq - apologies for any confusion...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top