简体   繁体   中英

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.

And another table that containing PRICE value for the group. And my goal is to get the price value or price value of the nearest PARENT group!

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 . And BIG T-SHIRT will be 88 USD and SMALL T-SHIRT 90 USD

All goods...........20 USD
--Shoes.............30 USD
--T-shirts........... 88 USD
----Small............ 90 USD
----Big................null
--Caps............... 33 USD
----Man caps.....null
------Classic......null
------Modern..... 200 USD
----Girls caps....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.

How to build a LINQ query? 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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