In an C# ASP.Net MVC project, I'm trying to make a List<string> from a LINQ variable.
Now this might be a pretty basic thing, but I just cannot get that to work without using the actual column names for the data in that variable. The thing is that in the interests of trying to make the program as dynamic as possible, I'm leaving it up to a stored procedure to get the data out. There can be any amount of any which way named columns depending on where the data is fetched from. All I care about is taking all of their values into a List<string>, so that I can compare user-input values with them in program.
Pointing to the columns by their names in the code means I'd have to make dozens of overloaded methods that all just basically do the same thing. Below is false non-functioning code. But it should open up the idea of what I mean.
// call for stored procedure var courses = db.spFetchCourseInformation().ToList(); // if the data fails a check on a single row, it will not pass the check bool passed = true; foreach (var i in courses) { // each row should be cast into a list of string, which can then be validated // on a row-by-row basis List courseRow = new List(); courseRow = courses[i]; // yes, obviously this is wrong syntax int matches = 0; foreach (string k in courseRow) { if (validator.checkMatch(courseRow[k].ToString())) { matches++; } } if (matches == 0) { passed = false; break; } }
Now below is an example of how I currently have to do it because I need to use the names for the columns
for (int i = 0; i < courses.Count; i++) { int matches = 0; if (validator.checkMatch(courses[i].Name)) matches++; if (validator.checkMatch(courses[i].RandomOtherColumn)) matches++; if (validator.checkMatch(courses[i].RandomThirdColumn)) matches++; if (validator.checkMatch(courses[i].RandomFourthColumn)) matches++; /* etc... * etc... * you get the point * and one of these for each and every possible variation from the stored procedure, NOT good practice * */
Thanks for help!