Parse and Linq

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am parsing a CSV string as follows:

var answers = CSVAnswers.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()).ToList();

I would like to create a List<Answer> which has 3 properties:
AnswerID, Answer, UpdatedDate

AnswerID is a Guid and I know how to get it.
Answer is taken from answers
UpdatedData is current date time and I also know how to get it

My question is if I can transform my expression to create directly the
list:

List<Answer> = CSVAnswers ...

Thanks,
Miguel
 
shapper said:
I am parsing a CSV string as follows:

var answers = CSVAnswers.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()).ToList();

I would like to create a List<Answer> which has 3 properties:
AnswerID, Answer, UpdatedDate

AnswerID is a Guid and I know how to get it.
Answer is taken from answers
UpdatedData is current date time and I also know how to get it

My question is if I can transform my expression to create directly the
list:

List<Answer> = CSVAnswers ...


It's almost there. You can write your values in the select in a Linq
query:

var answers = from s in CSVAnswers.Split(new char[] { ',' },...etc...)
select new Answer(guid, s.Trim(), DateTime.Now);
List<Answer> result = answers.ToList();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

String.Join and Linq 1
Linq Query. Please, help. Going crazy ... 1
Parse Array 2
Linq > Group 2
Empty string problem 6
Keep Order 2
Enumeration 2
Parsing XML 13

Back
Top