I have the following code:
public IList<Reference.Grid> GetGrid(string pk)
{
return (
from d in _referenceRepository.GetPk(pk)
select new Reference.Grid
{
PartitionKey = d.PartitionKey,
RowKey = d.RowKey,
Value = d.Value,
Order = d.Order
})
}
I am new to LINQ but I understand there are two ways I can write a select. Can someone help by telling me how I can rewrite this the other way. Also are there any differences and which way do people most often use?
The two ways you have mentioned are method- and query-syntax. You have used query-syntax.
This is the same in method syntax:
Note that i’ve changed it to use
ToListto create aList<Reference.Grid>. otherwise it would be anIEnumerable<Reference.Grid>.Use what is most readable and works. There are some methods not supported in query-syntax.
MSDN: LINQ Query Syntax versus Method Syntax (C#)
Which LINQ syntax do you prefer? Fluent or Query Expression