0

我需要根据外键值将多个 Linq 查询结果返回到 List 对象中。这样做的语法是什么?我是使用 Linq 的新手,所以下面是我迄今为止最好的猜测。我在 .Where()“子句”中收到一个错误,指出“名称 'pt' 在当前上下文中不存在。任何帮助将不胜感激!

        List<AgentProductTraining> productTraining = new List<AgentProductTraining>();

        var prodCodes = productTraining.Select(pt => new[]
                                                         {
                                                             pt.ProductCode,
                                                             pt.NoteId,
                                                             pt.ControlId
                                                         })
                                                         .Where(pt.CourseCode == course.CourseCode);
4

1 回答 1

1

您需要切换 where 的位置并选择是否使用扩展方法:

var prodCodes = productTraining.Where(pt => pt.CourseCode == course.CourseCode)
                               .Select(pt => new SomeRandomType
                                                         {
                                                             ProductCode = pt.ProductCode,
                                                             NoteId = pt.NoteId,
                                                             ControlId = pt.ControlId
                                                         });

正如您在上面看到的,我还建议您为该 select 语句创建一个类型,以便您不依赖匿名类型。你应该放入一个你知道一切的对象类型。

此外,如果 CourseCode 是一个字符串,那应该是pt.CourseCode.Equals(course.CourseCode).

于 2013-01-03T20:03:19.583 回答