How to join multiple tables?

前端 未结 1 1121
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 22:01

I have the following classes. I have a object var of Description class. I want to select Balance related to the Client provided in the var object u

相关标签:
1条回答
  • 2020-12-09 22:18

    You could try this:

    var balance = (from a in context.Accounts
                   join c in context.Clients on a.UserID equals c.UserID
                   where c.ClientID == yourDescriptionObject.ClientID
                   select a.Balance)
                  .SingleOrDefault();
    

    Or - if you only have the DescriptionID:

    var balance = (from a in context.Accounts
                   join c in context.Clients on a.UserID equals c.UserID
                   join d in context.Descriptions on c.ClientID equals d.ClientID
                   where d.DescriptionID == yourDescriptionID
                   select a.Balance)
                  .SingleOrDefault();
    

    (Or FirstOrDefault() or ToList() or Sum()? Because your model would allow that clients/descriptions are related to multiple accounts ...)

    0 讨论(0)
提交回复
热议问题