I have some queries that look like this:
public List<AnObjectModel> GetObjectFromDB(TheParameters)
{
using MyDataContext
{
var TheList = (....select new AnObjectModel()...).ToList();
return new List<AnObjectModel>(TheList);
}
}
And they work just fine.
My question is this: at the moment, I’m using var and then I’m doing a cast. Would doing this have any performance benefit?
public List<AnObjectModel> GetObjectFromDB(TheParameters)
{
using MyDataContext
{
List<AnObjectModel> TheList = (....select new AnObjectModel()...).ToList();
return TheList;
}
}
It would take me about 20 minutes to make the changes and I’m wondering if there’d be any difference.
Thanks.
You’re not using a cast – you’re creating a new list. That’s definitely pointless, given that you’ve already got a freshly-created
List<T>. I would just write:Or quite possibly:
That avoids having to bracket the query expression, which generally looks ugly.
Edit according to taste for layout, but definitely remove the redundant list creation.