我有两个列表,我必须从中获取两个值(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/