如何在 select 子句中创建匿名类型列表?

发布于 2024-10-26 03:30:07 字数 520 浏览 1 评论 0原文

我使用 LINQ to SQL 进行数据库操作。基本上我已经编写了一个返回学生类型的选择查询。学生类型中的一列是分数列表。

我能够构造没有标记的匿名学生类型,如下所示:

var studentList = from student in context.Student
                  from marks in context.Marks.Where(m => m.studentId = studentId)
                  select new
                  {
                     RollNo = student.RollNo,
                     Name = student.Name,
                     PhoneNo = student.PhoneNo
                  };

LINQ to SQL 中是否有可能在我的新匿名类型中创建匿名类型列表(在本例中为标记)?

I am using LINQ to SQL for my database operations. Basically I have written a select query which returns a Student Type. one of the column in the student type is list of marks.

I am able to construct the anonymous student type without marks as given below:

var studentList = from student in context.Student
                  from marks in context.Marks.Where(m => m.studentId = studentId)
                  select new
                  {
                     RollNo = student.RollNo,
                     Name = student.Name,
                     PhoneNo = student.PhoneNo
                  };

Is there a possibility in LINQ to SQL to create a list of anonymous types(marks in this case) in my new anonymous type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有深☉意 2024-11-02 03:30:07

选择所有有分数的学生我认为您会想要使用联接。 编辑:哎呀,您可能只需要每个学生一条记录。我添加了一个分组。

var studentList = from student in context.Student
                  join marks in Context.Marks
                      on student.studentId equals marks.studentId
                  group by student
                  into g
                  select new
                  {
                      RollNo = g.Key.RollNo,
                      Name = g.Key.Name,
                      PhoneNo = g.Key.PhoneNo,
                      Marks = g.marks
                  };

Selecting all student with marks I would think you would want to use a join. Edit: Oops, you probably only want one record per student. I've added a grouping.

var studentList = from student in context.Student
                  join marks in Context.Marks
                      on student.studentId equals marks.studentId
                  group by student
                  into g
                  select new
                  {
                      RollNo = g.Key.RollNo,
                      Name = g.Key.Name,
                      PhoneNo = g.Key.PhoneNo,
                      Marks = g.marks
                  };
月隐月明月朦胧 2024-11-02 03:30:07

如果您的 Marks 表上的 StudentId 是外键(如果不是,为什么不呢?),您应该能够执行以下操作:

var studentList = (from student in context.Student
                   select new
                   {
                       student.RollNo, // no need to explicitly name the properties if they're the same name as the property you're selecting
                       student.Name,
                       student.PhoneNo,
                       student.Marks // LINQ to SQL will do the join automagically
                   }).ToList();

我还假设您确实想要List - 要获取一个,您需要调用 .ToList()

If StudentId on your Marks table is a foreign key (and if not, why not?), you should just be able to do:

var studentList = (from student in context.Student
                   select new
                   {
                       student.RollNo, // no need to explicitly name the properties if they're the same name as the property you're selecting
                       student.Name,
                       student.PhoneNo,
                       student.Marks // LINQ to SQL will do the join automagically
                   }).ToList();

I'm also assuming you actually want a List<T> - to get one you need to call .ToList().

颜漓半夏 2024-11-02 03:30:07
var studentList = (from student in context.Student
                  select new
                  {
                     RollNo = student.RollNo,
                     Name = student.Name,
                     PhoneNo = student.PhoneNo,
                     Marks = context.Marks.Where(m => m.studentId = student.studentId)
                  }).ToList();
var studentList = (from student in context.Student
                  select new
                  {
                     RollNo = student.RollNo,
                     Name = student.Name,
                     PhoneNo = student.PhoneNo,
                     Marks = context.Marks.Where(m => m.studentId = student.studentId)
                  }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文