简体   繁体   中英

LINQ to EF Sub query

I have two linq queries, in the second query i use the first query as a sub query. Basically the first query does a groupby to return distinct rows of ContactID to use in the second query, which then does a groupby on idnumber to check for duplicates.

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

This is the error i get. I am using Firebird database.

Dynamic SQL Error SQL error code = -104 Token unknown - line 19, column 9 APPLY

EF generates SQL with a cross apply on sub query. I know this is not supported in later versions of Firebird. Any alternatives to this?

I think the first query could be eliminated (I guess contact.ID is a primary key), and use something like this:

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

Normally Any would be translated to SQL EXISTS subquery, which in turn most of the databases will treat as INNER JOIN . Unfortunatlely I don't know if Firebird does that, but it's worth trying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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