koundinya sarma Ответов: 2

Запрос Linq для консольного приложения


i have a code like

public class Student
{
public int StudentID { get; set; }
public string SName { get; set; }
public List<string> Course;
}

static List<Student> students = new List<Student>
{
new Student {StudentID=1, SName="koundinya"},
new Student {StudentID=2, SName="sarma"},
new Student {StudentID=3, SName="madhava"},
};

now my question is using LINQ can i add more courses to the students and no database and through console application only

????

What I have tried:

<pre lang="C#"><pre lang="C#">i have a code like

public class Student
{
public int StudentID { get; set; }
public string SName { get; set; }
public List&lt;string&gt; Course;
}

static List&lt;Student&gt; students = new List&lt;Student&gt;
{
new Student {StudentID=1, SName=&quot;koundinya&quot;},
new Student {StudentID=2, SName=&quot;sarma&quot;},
new Student {StudentID=3, SName=&quot;madhava&quot;},
};

2 Ответов

Рейтинг:
2

F-ES Sitecore

Не используя LINQ, просто обычный код

students.Add(new Student {StudentID=123, SName="John"});


Рейтинг:
18

#realJSOP

Вы не можете использовать LINQ для добавления данных.

Вы, вероятно, должны сделать что-то вроде этого:

Student student = students.Where(x=>x.StudentID == 1).FirstOrDefault();
if (student != null)
{
    if (studemt.Courses == null)
    {
        student.Courses = new List<string>();
    }
    student.Courses.AddRange(new string[]{"course #1", "course #1", "course #3",...});
}