score:8
Try this:-
var u = (from g in t.tblcourses
select new tblcourse { C_Id = g.C_Id, C_Name = g.C_Name }).ToList();
Instead of selecting anonymous type
, you can directly fill your custom Type.
score:4
Let LINQ create the tblcourse
objects instead of anonymous objects.
public List<tblcourse> GetData(string value)
{
return (from g in db.tblcourses
select new tblcourse() { C_Id = g.C_Id, C_Name = g.C_Name }).ToList();
}
score:1
If you mean how do you get it to look cleaner (as the performance gain is negligable), you can do so like this:
public List<tblcourse> GetData(string value)
{
return (from g in new testEntities1().tblcourses
select new tblcourse { C_Id = g.C_Id, C_Name = g.C_Name }).ToList();
}
score:4
I'd reduce it to something like:
Note: I've removed the unused variables. I've also assumed that testEntities1 is an Entity Framework DbContext and requires disposing. I've also used the results variable to temporarily hold a reference to the list so it can be easily debugged by adding a breakpoint.
public IList<tblcourse> GetData()
{
using (var testContext = new testEntities1())
{
var results =
testContext.tblcourses
.Select(c => new tblcourse() { C_Id = c.C_Id, C_Name = c.C_Name })
.ToList();
return results;
}
}
Of course replacing the foreach
with a LINQ
statement probably wont improve performance much, but it's probably more maintainable now.
You really want to look at a guide for best practices of naming class members in C#.
Source: stackoverflow.com
Related Articles
- increase Performance of the code
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- .NET String parsing performance improvement - Possible Code Smell
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Performance tuning C# permutations and SHA1 code
- creating Linq to sqlite dbml from DbLinq source code
- Replace nested foreach to Increase performance
- Are we compromising performance for achieving code readability when using LINQ?
- avoid loop to generate nullable collection, increase performance
- source code for LINQ 101 samples
- LINQ to SQL with a large database table increase performance
- How to increase performance of many foreach loop?
- Code performance on double for loop compare to linq/lambda
- List or Array of String Contain specific word in Html Source Code
- Is there a way to increase performance on these two foreach loops?
- What is the best way to increase the performance of this code?
- c# Linq or code to extract groups from a single list of source data
- increase performance of a linq query using contains
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Performance of Find() vs. FirstOrDefault()
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- LINQ performance FAQ
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- Does Linq OrderBy Not Sort Original Collection?
- How to group file contents into blocks based on the occurance of a string
- Retrieving entities with related tables c# using REST API2
- Group even and odd numbers
- The data reader has more than one field. Multiple fields are not valid for EDM primitive types
- LINQ: how to get an intersection of two sets of ints?
- Call Method from Linq query
- db4o, Linq, and UUID's
- Dynamic Linq Group By
- How could I use linq when calculating a answer from a symbol and two numbers
- Linq - Updating dynamic column
- Linq Groupby multiple columns with the same name (An anonymous type cannot have multiple properties with the same name)
- Generate linq classes using sqlmetal for npgsql
- How to invoke a UDF with LINQ in Cosmos DB SDK v3?
- Linq to Sql get today date only data from db
- Order list by average of child elements?
- File Upload read to memory and use as text file - is there a better way?
- NHibernate.Linq - Error querying when Nullable types in Where clause
- How to add where clause to ThenInclude
- Querying container with Linq + group by?