我有2个课程Users
和Userdetails
。
User
包含Userdetails ICollection
;
public class Users
{
public int Id;
public ICollection<Userdetails> Userdetails;
}
我想从UserDetails
对象中检索User
对象但找不到方法;
我已经用这种方式实现了它:
var lstUserdetails = new List<Userdetails>();
lstUserdetails = (from e in user.Userdetails
select e).ToList();
var userobject = ((Userdetails)lstUserdetails.Find(x=>x.id == Id));
但它显示userobject
null。
如何解决?
答案 0 :(得分:0)
var userDetailsObejct = (from u in user.UserDetails
where u.id == Id
select u).FirstOrDefault();
这将从具有等于Id的id的指定用户返回UserDetails集合中的UserDetails对象。如果找不到它,它将返回null。
答案 1 :(得分:-1)
试试这个
var lstUserdetails = new List<Userdetails>();
lstUserdetails = (from e in user.Userdetails where e.id == Id
select e).Firstordefault();