如何在 select 子句中创建匿名类型列表?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选择所有有分数的学生我认为您会想要使用联接。 编辑:哎呀,您可能只需要每个学生一条记录。我添加了一个分组。
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.
如果您的
Marks
表上的StudentId
是外键(如果不是,为什么不呢?),您应该能够执行以下操作:我还假设您确实想要
List
- 要获取一个,您需要调用.ToList()
。If
StudentId
on yourMarks
table is a foreign key (and if not, why not?), you should just be able to do:I'm also assuming you actually want a
List<T>
- to get one you need to call.ToList()
.