If you want to use Linq in your codesmith templates to filter, organize and sort your metadata the trick to ensure you have set the CompilerVersion to "v3.5" or "v4.0" etc.
Requires CodeSmith 5.1 or above.
Showing posts with label linq. Show all posts
Showing posts with label linq. Show all posts
Using linq to read csv file
Alternative title: Fastest way to read a csv file into objects
Alternative title 2: Its only one line of code!
File.ReadAllLines("Employees.csv")
.Select(x => x.Split(','))
.Select(x =>
new EmployeeObject
{
FirstName=x[0],
LastName=x[1],
DateOfBirth=DateTime.Parse(x[2]),
Department=x[3]
});
Alternative title 2: Its only one line of code!
File.ReadAllLines("Employees.csv")
.Select(x => x.Split(','))
.Select(x =>
new EmployeeObject
{
FirstName=x[0],
LastName=x[1],
DateOfBirth=DateTime.Parse(x[2]),
Department=x[3]
});
Convert from IEnumerable<gobject> to IEnumerable<x>
The easiest way to convert from IEnumerable<object> to a specific class e.g. IEnumerable<DomainObject> is by using the LINQ extension method Cast<T>.
IEnumerable<object> objEnum = GetData();
IEnumerable<DomainObject> domainObjEnum = objEnum.Cast<DomainObject>().ToList();
The same process can be used in reverse.
IEnumerable<object> objEnum = GetData();
IEnumerable<DomainObject> domainObjEnum = objEnum.Cast<DomainObject>().ToList();
The same process can be used in reverse.
Labels:
.net,
cast,
IEnumerable,
linq
Subscribe to:
Posts (Atom)
