从所有父母那里获得价值

时间:2014-05-26 08:45:52

标签: c# asp.net-mvc linq entity-framework

我一直在搜索,但无法找到答案。

我的数据库中有一个简单的表格,其中包含商店中的商品组。鞋子,T恤,帽子等。

另一个包含该组的PRICE值的表。我的目标是获得最近的PARENT组的价格或价格值!

例如,如果我想知道 CLASSIC MAN CAPS 的价格,它将 33 USD ,但 MODERN MAN CAPS 200 USD BIG T-SHIRT 88美元和小T恤 90美元

所有商品........... 20美元
--Shoes ............. 30美元
- T恤........... 88美元
----小............ 90美元
----大................空
- 买入............... 33美元
----男子帽..... null
------经典......空
------现代..... 200美元
----女孩帽.... 14美元

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,另一个是GroupId加入的PRICE VALUE。

如何构建LINQ查询?这样的东西,但有连接或递归?

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

我很高兴能够使用与上面代码相​​同的名称来编写代码示例。 Сlarify再次:当前组的值或如果它是最近的PARENT组的空值。

1 个答案:

答案 0 :(得分:1)

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

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