Linq查询控制台应用程序 [英] Linq query for console application

查看:45
本文介绍了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;},
};

推荐答案

您无法使用LINQ添加数据。 />


您可能需要做以下事情:



You can't use LINQ to add data.

You probably have to do something like this:

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",...});
}


不使用LINQ,只需普通代码



Not using LINQ, just normal code

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


这篇关于Linq查询控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆