Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do alias in Select method (LINQ)

Tags:

c#

linq

I'm trying to alias the string list with a named column:

var providers = EMRRepository.GetProviders().Select(x => x as name);

where 'GetProviders()' returns a List<string>

like image 385
Chris Hayes Avatar asked Jun 20 '11 23:06

Chris Hayes


People also ask

How does Select work in LINQ?

LINQ Select operator is used to return an IEnumerable collection of items, including the data performed on the transformation of the method. By using Select Operator, we can shape the data as per our needs. In it, we can use two syntax types; let's see each method working flow.

How do I write a selection in LINQ?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.

What does Select () do in C#?

In a query expression, the select clause specifies the type of values that will be produced when the query is executed. The result is based on the evaluation of all the previous clauses and on any expressions in the select clause itself.

What is the difference between where and Select in LINQ?

In case of Select it you can map to an IEnumerable of a new structure. Where() works as an filter to the IEnumerable, it will return the result on the basis of the where clause.


2 Answers

It's called a "Projection", just select a new anonymous type.

var projection = data.Select( x => new { FieldName = x.Property } );
like image 56
Brandon Moretz Avatar answered Oct 19 '22 08:10

Brandon Moretz


You are looking to select into a new anonymous type.

var providers = EMRRepository.GetProviders().Select(x => new { Name = x });
like image 20
Anthony Pegram Avatar answered Oct 19 '22 06:10

Anthony Pegram