LINQ with EF Core 返回重复结果
LINQ with EF Core returning duplicated results
我正在使用 Entity Framework Core 和 LINQ 来执行一些连接,但它没有按预期发生,如果 select 两个对象,它 return 是重复的,如果我select 只有一个人,phone 列表为空。我有我的对象:
public class Person{
private string _Name;
private string _CompanyTradeName;
[Key, Column("pID")]
public uint PersonID { get; set; }
[Column("pUUID")]
public Guid PersonUUID { get; set; }
[Column("pType")]
public string Type { get; set; }
[Column("pCrptName")]
public string Name {
get { return Encryption.DecryptString(_Name); }
set { _Name = value; }
}
[Column("pCrptCompanyTradeName")]
public string CompanyTradeName {
get { return (_CompanyTradeName != null) ? Encryption.DecryptString(_CompanyTradeName) : null; }
set { _CompanyTradeName = value; }
}
[Column("pProfilePicture")]
public string ProfilePicture { get; set; }
[Column("pStatus")]
public bool Status { get; set; }
public virtual ICollection<PersonAddress> PersonAddresses { get; set; }
public virtual ICollection<PersonDoc> PersonDocs { get; set; }
public virtual ICollection<PersonPhone> PersonPhones { get; set; }
}
我的个人电话 class:
public class PersonPhone {
private string _PhoneNumber;
[Key, Column("ppID")]
public uint PersonPhoneID { get; set; }
[Column("ppUUID")]
public Guid PersonPhoneUUID { get; set; }
[Column("pID")]
public uint PersonID { get; set; }
public PhoneType PhoneTypes { get; set; }
[Column("ptID")]
public uint PhoneTypeID { get; set; }
[Column("ppCrptPhoneNumber")]
public string PhoneNumber { get { return Encryption.DecryptString(_PhoneNumber); } set { _PhoneNumber = value; } }
}
我正在尝试 return PersonPhone 加入 Person,但它 returning null:
var query = from p in _context.Person
join pp in _context.PersonPhone on p.PersonID equals pp.PersonID
select p;
return Ok(await query.FirstOrDefaultAsync());
有人可以帮我解决这个问题吗?
你可以试试
var personId=...
var person= await _context.Person
.Where(p=>p.PersonID=personId
.Include(p=> p.PersonPhones)
.FirstOrDefaultAsync());
return Ok(person);
或者如果您只想要手机
var personId=...
var phones = await _context.PersonPhones
.Where(p=>p.PersonID=personId
.Include(p=> p.Person)
.ToListAsync());
return Ok(phones);
但在这种情况下,您将不得不修复 class
public class PersonPhone {
private string _PhoneNumber;
[Key, Column("ppID")]
public uint PersonPhoneID { get; set; }
[Column("ppUUID")]
public Guid PersonPhoneUUID { get; set; }
[Column("pID")]
public uint PersonID { get; set; }
public virtual Person Person {get; set;}
.....
}
我正在使用 Entity Framework Core 和 LINQ 来执行一些连接,但它没有按预期发生,如果 select 两个对象,它 return 是重复的,如果我select 只有一个人,phone 列表为空。我有我的对象:
public class Person{
private string _Name;
private string _CompanyTradeName;
[Key, Column("pID")]
public uint PersonID { get; set; }
[Column("pUUID")]
public Guid PersonUUID { get; set; }
[Column("pType")]
public string Type { get; set; }
[Column("pCrptName")]
public string Name {
get { return Encryption.DecryptString(_Name); }
set { _Name = value; }
}
[Column("pCrptCompanyTradeName")]
public string CompanyTradeName {
get { return (_CompanyTradeName != null) ? Encryption.DecryptString(_CompanyTradeName) : null; }
set { _CompanyTradeName = value; }
}
[Column("pProfilePicture")]
public string ProfilePicture { get; set; }
[Column("pStatus")]
public bool Status { get; set; }
public virtual ICollection<PersonAddress> PersonAddresses { get; set; }
public virtual ICollection<PersonDoc> PersonDocs { get; set; }
public virtual ICollection<PersonPhone> PersonPhones { get; set; }
}
我的个人电话 class:
public class PersonPhone {
private string _PhoneNumber;
[Key, Column("ppID")]
public uint PersonPhoneID { get; set; }
[Column("ppUUID")]
public Guid PersonPhoneUUID { get; set; }
[Column("pID")]
public uint PersonID { get; set; }
public PhoneType PhoneTypes { get; set; }
[Column("ptID")]
public uint PhoneTypeID { get; set; }
[Column("ppCrptPhoneNumber")]
public string PhoneNumber { get { return Encryption.DecryptString(_PhoneNumber); } set { _PhoneNumber = value; } }
}
我正在尝试 return PersonPhone 加入 Person,但它 returning null:
var query = from p in _context.Person
join pp in _context.PersonPhone on p.PersonID equals pp.PersonID
select p;
return Ok(await query.FirstOrDefaultAsync());
有人可以帮我解决这个问题吗?
你可以试试
var personId=...
var person= await _context.Person
.Where(p=>p.PersonID=personId
.Include(p=> p.PersonPhones)
.FirstOrDefaultAsync());
return Ok(person);
或者如果您只想要手机
var personId=...
var phones = await _context.PersonPhones
.Where(p=>p.PersonID=personId
.Include(p=> p.Person)
.ToListAsync());
return Ok(phones);
但在这种情况下,您将不得不修复 class
public class PersonPhone {
private string _PhoneNumber;
[Key, Column("ppID")]
public uint PersonPhoneID { get; set; }
[Column("ppUUID")]
public Guid PersonPhoneUUID { get; set; }
[Column("pID")]
public uint PersonID { get; set; }
public virtual Person Person {get; set;}
.....
}