[英]Get value from all parents
我已经搜索了很多,但是找不到答案。
我的数据库中有一个简单的表,其中包含商店中的商品组。 鞋子,T恤,帽子等
另一个包含该组PRICE值的表。 我的目标是获取最近的PARENT组的价格值或价格值!
例如,如果我想知道CLASSIC MAN CAPS的价格,它将是33美元,而MODERN MAN CAPS的价格是200美元 。 BIG T-SHIRT将为88美元 ,SMALL T-SHIRT将为90美元
所有商品........... 20 USD
-鞋子............. 30美元
--T恤........... 88美元
---- Small ............ 90美元
----大................空
--Caps ...... 33美元
----男帽....
------经典......空
------现代..... 200美元
----女童帽.... 14 USD
类
public class Groups
{
public Groups()
{
Goods = new HashSet<Goods>();
PriceFormation = new HashSet<PriceFormation>();
ChildGroups = new HashSet<Groups>();
}
[ForeignKey("Accounts")]
public long AccountId { get; set; }
[Key]
public long Id { get; set; }
public long? ParentId { get; set; }
public string Name { get; set; }
public virtual ICollection<Goods> Goods { get; set; }
[ForeignKey("AccountId")]
public virtual Accounts Accounts { get; set; }
public virtual ICollection<PriceFormation> PriceFormation { get; set; }
public virtual ICollection<Groups> ChildGroups { get; set; }
[ForeignKey("ParentId")]
public virtual Groups ParentGroups { get; set; }
}
public class PriceFormation
{
[ForeignKey("Accounts")]
public long AccountId { get; set; }
[ForeignKey("Groups")]
public long GroupId { get; set; }
[ForeignKey("Shops")]
public long ShopId { get; set; }
[ForeignKey("Goods")]
public long GoodId { get; set; }
[Key]
public long Id { get; set; }
[ForeignKey("AccountId")]
public virtual Accounts Accounts { get; set; }
[ForeignKey("GroupId")]
public virtual Groups Groups { get; set; }
[ForeignKey("ShopId")]
public virtual Shops Shops { get; set; }
[ForeignKey("GoodId")]
public virtual Goods Goods { get; set; }
public long Kind { get; set; }
public Decimal Value { get; set; }
}
这是2个表,一个表包含GROUPS,另一个表包含PRICE VALUE,该价格由GroupId连接。
如何建立LINQ查询? 像这样,但有联接或递归吗?
_db.PriceFormation.FirstOrDefault(m => m.GroupId == 18);
我将很高兴获得与上面的代码同名的工作代码示例。 再次确认:当前组的值,或者如果它为null,则为最近的PARENT组。
您可以使用下面提到的代码
var abc = _db.Groups.Where(p => _db.PriceFormation.Any(q => q.GroupId == p.Id));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.