Compare a list element to string value

+2 Chris Blum · September 3, 2014
Hello.
I have a list of strings and I only want to write to file if the item in the list matches a particular string value. This is writing everything but I want the information only if the student name matches. Help please. Here is the code.

for (var i = 0; i < listLessonDate.Count; i++) {
if (listStudentLastName.Any(s=>listStudentLastName.Contains("Adams")))
 { writer.WriteLine("{0}", "Lesson Date: " + listLessonDate);
//Console.WriteLine("{0}", listStopDate);
writer.WriteLine("{0}", "Student Name: " + "\r\n" + listStudentLastName + "," + listStudentFirstName);
 //Console.WriteLine("{0}", "Student First Name: " + listStudentFirstName);
writer.WriteLine("{0}", "Instructor Last Name: " + "\r\n" + listInstructorLastName + "," + listInstructorFirstName);
//Console.WriteLine("{0}", "Instructor First Name: " + listInstructorFirstName);
writer.WriteLine("{0}", "Course: " + "\r\n" + listCourseName);
writer.WriteLine("{0}", "Team: " + "\r\n" + listTeamName);
writer.WriteLine("{0}", "Lesson: " + "\r\n" + listLessonName);
writer.WriteLine("{0}", "Group: " + "\r\n" + listGroupName);
writer.WriteLine("{0}", "Lesson Status: " + "\r\n" + listLessonStatus);
writer.WriteLine("{0}", "Lesson Cancellation Reason: " + "\r\n" + listLessonCancelledReason);
 writer.WriteLine("\r\n"); } }

Post a Reply

Replies

Oldest  Newest  Rating
-1 George Williams · September 9, 2014
Ah yes I understand now. Good luck then .
0 Chris Blum · September 9, 2014
George,
The code i've posted on here is just a test code. I'm importing from an external sql database to a csv file. I just put up a generic name so as not to post a students name in a public forum for no reason.
Eventually i'll be moving the code to windows form so it looks nice for the instructors and the program will provide them with different search options.
0 George Williams · September 8, 2014
It seems you've found a fix to your problem or maybe not. I dont know, but what I do know is that from the code your showing me, your not specifying 1  person with the name Adam but everyone with their name containing adam(Forgive me if I'm wrong, I rarely work with databases).

What I would suggest though if you still need help is to add a Console.ReadLine() and then use what is Inputted to search through your database for that name, for this I would recommend using Microsoft Acess and C# Windows Forms instead of console
0 Chris Blum · September 8, 2014
George,
Thank you for the response.
This program is going to pull in thousands of records of information so i'd like for my instructors to be able to type a name and only pull records for that particular student. After the program gets the data for that student i'm going to make some graphs so they can check their progress.

I'd prefer to have all the information grabbed as the program opens and then allow the teacher to search the data (they will do reports on multiple different students or classes as the program is open).

I don't understand why my code is printing everything if the criteria matches instead of just the 'i' element for from each of the selected lists.

I've also tried this code but the program is still writing every element.

for (var i = 0; i < listLessonDate.Count; i++)
{ if (listStudentLastName.Contains("Adams")) {.....doing stuff with data.....}}

Thanks for the help,
Chris Blum

***FIXED***
Got this to work by adding the following line of code before the if statement but inside the for loop

string nameCheck = listStudentName;
0 George Williams · September 7, 2014
"Hello.

I have a list of strings and I only want to write to file if the item in the list matches a particular string value. This is writing everything but I want the information only if the student name matches. Help please. Here is the code.

"
That's what your saying, Your code could be much less 'complicated' if you used something like:
List<string> StudentsName = new List<string>();
StudentsName.Add("Adam");




Unfortunately due to limited information that I am given this below may not be correct(Add this to the above Code btw):



Console.ReadLine();

if(Console.ReadLine() == StudentsName[0])
{
Console.WriteLine("Adam is a A+ Student");
}

But this all under the impression that you'd like to just print a line about the student, If Im wrong I'd be happy to help more :)
  • 1

C#

107,007 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators