C# 使用Where子句返回子级
我在我的Xamarin项目中使用。我试图使用where子句返回我模型的子元素。使用网站上的示例模型:C# 使用Where子句返回子级,c#,sqlite,sqlite-net,sqlite-net-extensions,C#,Sqlite,Sqlite Net,Sqlite Net Extensions,我在我的Xamarin项目中使用。我试图使用where子句返回我模型的子元素。使用网站上的示例模型: public class Stock { [PrimaryKey, AutoIncrement] public int Id { get; set; } [MaxLength(8)] public string Symbol { get; set; } [OneToMany(CascadeOperations = CascadeOperation.All
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
}
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))] // Specify the foreign key
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
[ManyToOne] // Many to one relationship with Stock
public Stock Stock { get; set; }
}
返回时,所有股票参数都为空。然后,我可以循环遍历每个结果并设置它们,但我认为在原始查询中一定有更好的方法来实现这一点。您可以获得任何对象调用
GetChildren
方法的关系:
var results = db.Table<Valuation>().Where(x=>x.Price > 5.0m).ToList();
foreach (var element in results) {
conn.GetChildren(element);
}
请注意,您无法访问此查询中的关系,因为它们需要未执行的连接。对于这样的简单查询,它应该按预期工作
var results = db.Table<Valuation>().Where(x=>x.Price > 5.0m).ToList();
var results = db.Table<Valuation>().Where(x=>x.Price > 5.0m).ToList();
foreach (var element in results) {
conn.GetChildren(element);
}
var results = conn.GetAllWithChildren<Valuation>(x => x.Price > 5.0m).ToList();