locked
Join Query with group and conditions RRS feed

  • Question

  • I want to execute following sql using linq in c#

    select sum(EDAmount)
    from accounts a
    inner join entriesdetail ed on a.Acode =ed.acode
    inner join entries e on ed.eid = e.eid and e.edate > '1/1/2009'
    group by ed.acode


    Following are the entities

    Accounts
    - ACode

    Entry
    - EID
    - EDate

    EntryDetail
    - EDID
    - EID
    - EDAmound
    - ACode
    Monday, May 25, 2009 11:49 AM

Answers

  • A fairly literal translation should work:

    from a in db.Accounts
    from ed in db.EntriesDetail where a.Acode == ed.Acode
    from e in db.Entires where ed.eid == eid && e.Edate > new DateTime (2009, 1, 1)
    group a by ed.acode into g
    select g.Sum (a => a.EDAmount)

    It would be a cleaner, though, with association properties.

    Joe



    Write LINQ queries interactively - www.linqpad.net
    • Marked as answer by Harry Zhu Monday, June 1, 2009 10:06 AM
    Monday, May 25, 2009 2:41 PM