我有两个列表,我必须从中获取两个值(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找到。 即我必须找到myUserFields.FieldName,它等于otherUserField.FieldName并将其分配给MyValue。
可以在单个查询中完成,如上所述吗?请建议
答案 0 :(得分:3)
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();
答案 1 :(得分:0)
根据您的问题,您不想清楚要分配给MyValue。
以下将分配myuser_field对象,该对象的FieldName与当前枚举的myuser_object(otherUserField.FieldName)的FieldName匹配:
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 = myUserFields.Single(uf => uf.FieldName == otherUserField.FieldName)
}).ToList();