Filter base entity from child entities' properties

六月ゝ 毕业季﹏ 提交于 2020-01-06 14:54:09

问题


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

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");

It is not allowed. I can only getwhat I want by

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...

Any help would be appreciated.


回答1:


Edit

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.

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

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

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>();

Everything seems to work fine for me.



来源:https://stackoverflow.com/questions/9101971/filter-base-entity-from-child-entities-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!