简体   繁体   English

基于实体框架中的某些过滤器的动态连接多个实体

[英]Dynamic Join of multiple entities based on some filter in Entity Framework

I am pretty new to Entity Framework and LINQ and I have an entity with more than 10+ other associated entities (one-to-many relationships). 我对Entity Framework和LINQ还是很陌生,我有一个包含10多个其他关联实体(一对多关系)的实体。 Now, I'm planning to make a search page in my application in which users could select which fields (ie those 10+ tables) they want to be considered when searching. 现在,我打算在我的应用程序中创建一个搜索页面,用户可以在其中选择要在搜索时考虑的字段(即那10个以上的表)。

Now, I'm trying to write a query to achieve the above goal. 现在,我正在尝试编写查询以实现上述目标。 Any help how I could sort this out using LINQ method syntax? 有什么帮助我可以使用LINQ方法语法来解决这个问题吗? I mean, to write a multiple join query based on user's choice. 我的意思是,根据用户的选择编写多联接查询。 (ie which of Class1, Class2, ... to join with main Entity to finally have all the related fields in one place). (即,与主实体一起加入的Class1,Class2,...中的哪一个最终将所有相关字段放在一个位置)。 Below is a sample code (Just a hunch, in fact) 以下是示例代码(实际上,这只是预感)

if(somefilter#1)
result = db.Companies.Join(db.Channels, p => p.Id, k => k.CId,
                                       (p, k) => new {Company = p, Channels=k});
if(somefilter#2)
result = result.Join(db.BusinnessType, ........);

if(somefilter#3)
result = result.Join(db.Values, .......);

For complex queries it may be easier to use the other LINQ notation. 对于复杂的查询,使用其他LINQ表示法可能会更容易。 You could join multiple entities like this: 您可以像这样加入多个实体:

from myEntity in dbContext.MyEntities
join myOtherEntity in dbContext.MyOtherEntities on myEntity.Id equals myOtherEntity.MyEntityId
join oneMoreEntity in dbContext.OneMoreEntities on myEntity.Id equals oneMoreEntity.MyEntityId
select new {
    myEntity.Id,
    myEntity.Name,
    myOtherEntity.OtherProperty,
    oneMoreEntity.OneMoreProperty
}

You can join in other entities by adding more join statements. 您可以通过添加更多的join语句来加入其他实体。 You can select properties of any entity from your query. 您可以从查询中选择任何实体的属性。 The example I provided uses a dynamic class, but you can also define a class (like MyJoinedEntity ) into which you can select instead. 我提供的示例使用一个动态类,但是您也可以定义一个类(例如MyJoinedEntity ),您可以在该类中进行选择。 To do it you would use something like: 为此,您将使用类似以下内容的方法:

...
select new MyJoinedEntity {
    Id = myEntity.Id,
    Name = myEntity.Name,
    OtherProperty = myOtherEntity.OtherProperty,
    OneMoreProperty = oneMoreEntity.OneMoreProperty
}

EDIT: 编辑:

In case when you want to have conditional joins you can define MyJoinedEntity with all the properties you will need if you were to join everything. 如果要进行条件连接,则可以定义MyJoinedEntity以及所有连接MyJoinedEntity的所有属性。 Then break up the join into multiple methods. 然后将联接分解为多种方法。 Like this: 像这样:

public IEnumerable<MyJoinedEntity> GetEntities() {
    var joinedEntities = from myEntity in dbContext.MyEntities
        join myOtherEntity in dbContext.MyOtherEntities on myEntity.Id equals myOtherEntity.MyEntityId
        join oneMoreEntity in dbContext.OneMoreEntities on myEntity.Id equals oneMoreEntity.MyEntityId
            select new MyJoinedEntity {
                Id = myEntity.Id,
                Name = myEntity.Name,
                OtherProperty = myOtherEntity.OtherProperty,
                OneMoreProperty = oneMoreEntity.OneMoreProperty
            };

    if (condition1) {
        joinedEntities = JoinWithRelated(joinedEntities);
    }

}

public IEnumerable<MyJoinedEntity> JoinWithRelated(IEnumerable<MyJoinedEntity> joinedEntities) {
    return from joinedEntity in joinedEntities
    join relatedEntity in dbContext.RelatedEntities on joinedEntity.Id equals relatedEntity.MyEntityId
        select new MyJoinedEntity(joinedEntity) {
            Comments = relatedEntity.Comments
        };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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