簡體   English   中英

從所有父母那里獲得價值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM