LINQ to EF Sub查询

时间:2015-10-27 08:51:22

标签: c# entity-framework linq firebird

我有两个linq查询,在第二个查询中我使用第一个查询作为子查询。基本上,第一个查询执行groupby以返回在第二个查询中使用的不同行的ContactID,然后在idnumber上执行groupby以检查重复项。

private IQueryable<Contact> GetDistinctContact()
{

    IQueryable<Contact> query = (from contact in context.Contacts
                                 where contact.EDITED.Equals(0)
                                 && contact.NOTACTIVE.Equals(false)
                                 && contact.ID > 10001
                                 join client in context.Clients on new { ClientID = contact.ID, EDITED = 0, DELETED = false }
                                 equals new { ClientID = client.ContactID, EDITED = client.EDITED, DELETED = client.DELETED }
                                 join member in context.Members on new { MemberID = client.ID, EDITED = 0, DELETED = false }
                                 equals new { MemberID = member.ClientID, EDITED = member.EDITED, DELETED = member.DELETED }
                                 select contact);

    return query.GroupBy(x => x.ID).Select(grp => grp.FirstOrDefault());
}


IQueryable<ContactDetailsViewModelPart> query = (from contact in GetDistinctContact()
                                                 where contact.IdNumber != null
                                                 && !contact.IdNumber.Trim().Equals("")
                                                 && contact.EDITED.Equals(0)
                                                 && contact.NOTACTIVE.Equals(false)
                                                 && contact.ID > 10001
                                                 group contact
                                                 by new ContactDetailsViewModelPart
                                                 {
                                                     IDNumber = contact.IdNumber,
                                                     LastName = contact.LastName
                                                 }
                                                 into idNumberGroup
                                                 where idNumberGroup.Count() > 1
                                                 select new ContactDetailsViewModelPart
                                                 {
                                                    IDNumber = idNumberGroup.Key.IDNumber,
                                                    LastName = idNumberGroup.Key.LastName
                                                });

return query.ToList();   

这是我得到的错误。我正在使用Firebird数据库。

动态SQL错误 SQL错误代码= -104 令牌未知 - 第19行,第9列 应用

EF通过对子查询的交叉应用生成SQL。我知道Firebird的更高版本不支持此功能。有什么替代品吗?

1 个答案:

答案 0 :(得分:1)

我认为可以消除第一个查询(我猜contact.ID是主键),并使用类似这样的内容:

var query =
    from contact in context.Contacts
    where contact.IdNumber != null
    && !contact.IdNumber.Trim().Equals("")
    && contact.EDITED.Equals(0)
    && contact.NOTACTIVE.Equals(false)
    && contact.ID > 10001
    && context.Clients.Any(client =>
        client.ContactID == contact.ID && client.EDITED == contact.EDITED && client.DELETED == contact.EDITED
        && context.Members.Any(member =>
            member.ClientID == client.ID && member.EDITED == client.EDITED && member.DELETED == client.DELETED
        )
    )
    group contact
    by new ContactDetailsViewModelPart
    {
        IDNumber = contact.IdNumber,
        LastName = contact.LastName
    }
    into idNumberGroup
    where idNumberGroup.Count() > 1
    select idNumberGroup.Key;
return query.ToList();

通常Any将被转换为SQL EXISTS子查询,而大多数数据库将被视为INNER JOIN。不幸的是,我不知道Firebird是否能做到这一点,但值得一试。