asp.net - 查询中的 Linq 子查询

标签 asp.net linq c#-4.0

我有两个列表,我必须从中获取两个值(MyCaption 和 MyValue)。

List<UserInfoModel> userInfo = new List<UserInfoModel>();

List<myuser_field> myUserFields = GetMyUserFields();
var otherUserFields = otherUserService.GetOtherUserFields();

userInfo = (from otherUserField in otherUserFields
               where otherUserField.Chosen == true 
                    select new UserInfoModel {
                        MyCaption = otherUserField.FieldAlias,
                        MyValue =
                    }).ToList();

MyCaption 我直接从列表之一获取。现在要获取 MyValue,我需要根据 otherUserField.FieldName 进行查找。 即我必须找到等于 otherUserField.FieldName 的 myUserFields.FieldName 并将其分配给 MyValue。

可以在上面的单个查询中完成吗?请推荐

最佳答案

userInfo = (from myUserField in myUserFields
            join otherUserField in otherUserFields
            on myUserField.FieldName == otherUserField.FieldName
            where otherUserField.Chosen == true 
            select new UserInfoModel {
                MyCaption = otherUserField.FieldAlias,
                MyValue = myUserField.FieldName
            }).ToList();

关于asp.net - 查询中的 Linq 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8926293/

相关文章:

javascript - 使用 AngularJS 路由的 ASP.NET 5 路由

c# - 我正在使用大量数据进行比较。是否有将数据分页到数据表的想法

c# - 从另一个类引用数据库上下文类

c# - .NET 4.0 比早期版本慢,这是真的吗?

c# - 在 ASP.NET MVC 应用程序中使用静态存储库的优缺点

asp.net - 用于调试 VB 脚本代码的工具或 IDE

c# - 您如何将与集合中其他项目具有依赖性的项目分组?

c# - 从数据库表中选择两行

c# - 如何优化此 Linq 查询以查找过去 24 小时内浏览次数最多的博客文章

c# - 使用 LINQ 将缺失的项目添加到集合中