从子实体的属性过滤基础实体 [英] Filter base entity from child entities' properties

查看:155
本文介绍了从子实体的属性过滤基础实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个解决方案几天。

I have been looking for a solution for days now.

我的应用程序中有八个实体,一个基数和七个从该基础实体继承的实体。一些子实体具有相同的属性。

I have eight entities in my application, a base and seven entities that inherits from this base entity. Some of the child entities have same properties.

public class LogEntry(){
    public int LogEntryId{get;set;}
    public string ...
}

public class TestEntry : LogEntry{
   ....
   public string SomeProperty{get;set;} //SomePropertyThatIsNotInBaseClass
   ....
}
public class XEntry : LogEntry{
   ....
   public string SomeProperty{get; set;}
   ....
}

我试图过滤这个SomeProperty的基础实体。我正在尝试一个查询,如

I am trying to filter base entity by this SomeProperty. I am trying to a query like

var value = Db.LogEntry.Where(i=>i.SomePropery == "some string");

不允许。我只能通过

IQueryable<LogEntry> first = Db.LogEntry.OfType<TestEntry>.Where(i=>i.SomeProperty == "...");
IQueryable<LogEntry> second = Db.LogEntry.OfType<XEntry>.Where(i=>i.SomeProperty == "...");
...

最后连接它们。有比这种方法更聪明的做法吗?扩展方法等...

And concatenate them at the end. Is there more clever way to do than this method? Extension methods etc...

任何帮助将不胜感激。

推荐答案

p> 修改

更仔细地看你的例子,我不认为你想做的是可能的。如果您正在针对BASE实体类型编写查询,则只能查询基类型中定义的字段。

Looking at your example more closely, I don't think what you are trying to do is possible. If you are writing a query against the BASE entity type, then you can only query fields that are defined in the base type.

由于LogEntry中未定义SomeProperty ,你不能写这个查询:

Since "SomeProperty" is not defined in LogEntry, you cannot write this query:

var logEntries = db.LogEntry.Where(r => r.SomeProperty == "foo");

由于LogEntry类中未定义SomeProperty。

Because SomeProperty is not defined in the LogEntry class.

如果要针对基类编写查询,则需要执行以下操作:

If you want to write queries against the base class then you need to do something like the following:

public class TPTContext : DbContext
{
    public TPTContext() : base("name=TPT")
    { }

    public DbSet<BillingDetail> BillingDetails { get; set; }
}

public abstract class BillingDetail
{
    public int BillingDetailId { get; set; }
    public string Owner { get; set; }
    public string Number { get; set; }
}

[Table("BankAccounts")]
public class BankAccount : BillingDetail
{
    public string BankName { get; set; }
    public string Swift { get; set; }
}

[Table("CreditCards")]
public class CreditCard : BillingDetail
{
    public int CardType { get; set; }
    public string ExpiryMonth { get; set; }
    public string ExpiryYear { get; set; }
}

我对基类编写了以下查询:

I wrote the following query against the base class:

TPTContext db = new TPTContext();

var allAccounts = db.BillingDetails.Where(b => b.Owner == "boran");
var bankAccounts = allAccounts.OfType<BankAccount>();
var creditCards = allAccounts.OfType<CreditCard>();

一切似乎都适用于我。

这篇关于从子实体的属性过滤基础实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆