简体   繁体   English

从所有父母那里获得价值

[英]Get value from all parents

I have been searching a lot, but cant find the answer. 我已经搜索了很多,但是找不到答案。

I have a simple table in my DB that contains the groups of goods in the shop. 我的数据库中有一个简单的表,其中包含商店中的商品组。 Shoes,t-shirts, caps etc. 鞋子,T恤,帽子等

And another table that containing PRICE value for the group. 另一个包含该组PRICE值的表。 And my goal is to get the price value or price value of the nearest PARENT group! 我的目标是获取最近的PARENT组的价格值或价格值!

For example if i want to know the price for CLASSIC MAN CAPS it will be 33 USD but for MODERN MAN CAPS 200 USD . 例如,如果我想知道CLASSIC MAN CAPS的价格,它将是33美元,MODERN MAN CAPS的价格是200美元 And BIG T-SHIRT will be 88 USD and SMALL T-SHIRT 90 USD BIG T-SHIRT将为88美元 ,SMALL T-SHIRT将为90美元

All goods...........20 USD 所有商品........... 20 USD
--Shoes.............30 USD -鞋子............. 30美元
--T-shirts........... 88 USD --T恤........... 88美元
----Small............ 90 USD ---- Small ............ 90美元
----Big................null ----大................空
--Caps............... 33 USD --Caps ...... 33美元
----Man caps.....null ----男帽....
------Classic......null ------经典......空
------Modern..... 200 USD ------现代..... 200美元
----Girls caps....14 USD ----女童帽.... 14 USD

Classes

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; }
}

This is 2 tables, one with GROUPS and another one contains PRICE VALUE joined by GroupId. 这是2个表,一个表包含GROUPS,另一个表包含PRICE VALUE,该价格由GroupId连接。

How to build a LINQ query? 如何建立LINQ查询? Something like this but with joins or recursion? 像这样,但有联接或递归吗?

_db.PriceFormation.FirstOrDefault(m => m.GroupId == 18);

I would be very happy get working code sample with the same names as in code above. 我将很高兴获得与上面的代码同名的工作代码示例。 Сlarify once again: value for current group or if it is null the nearest PARENT group. 再次确认:当前组的值,或者如果它为null,则为最近的PARENT组。

您可以使用下面提到的代码

var abc = _db.Groups.Where(p => _db.PriceFormation.Any(q => q.GroupId == p.Id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM