1

I'm new in Linq, and I want to convert this sql Query to Linq Format.

This is the SQL format

select *
from investorwallets iw
where transactionid in 
(select investordepositid from investordeposits)
or transactionid in
(select withdrawalid from investorwithdrawals)
or transactionid in
(select paymentdistributionid from paymentdistributions)

I've looked on this SO Question too, but no luck for me

EDIT

This is what I have tried. I use Linqpad for testing it

from iw in  Investorwallets 
where   (
            from id in Investordeposits // I got error from here
            select id.investordepositid
        )

Anyone can help me?

Thank you

1
  • 1
    Add .Contains(iw.transactionid). Then do the similar for other criterias with || between and you are done. Commented Oct 18, 2016 at 12:06

2 Answers 2

1

The most direct is:

from iw in investorwallets 
where investordeposits.Any(iten => item.investordepositid == iw.transactionid) ||
      investorwithdrawals.Any(iten => item.withdrawalid == iw.transactionid) ||
      paymentdistributions.Any(item => item.paymentdistributionid == iw.transactionid)
select iw;

However you can also union the results and then do .Contains:

var ids = investorwithdrawals.Select(item => item.investordepositid)
                             .Union(investorwithdrawals.Select(item => item.withdrawalid))
                             .Union(paymentdistributions.Select(item => item.paymentdistributionid));

var result = investorwallets.Where(item => ids.Contains(item.transactionid));
Sign up to request clarification or add additional context in comments.

1 Comment

hi dude, will try it, and will update to you soon, thanks
1
        List<investorwallet> investorwallets = GetInvestorwallets();
        List<investordeposit> investordeposits = GetInvestordeposits();
        List<investorwithdrawal> investorwithdrawals = GetInvestorwithdrawals();
        List<paymentdistribution> paymentdistributions = GetPaymentdistribution();

        List<investorwallet> newList = investorwallets.Where(x => investordeposits.Any(y=>y.investordepositid == x.transactionid)
                                        || investorwithdrawals.Any(y => y.withdrawalid == x.transactionid)
                                        || paymentdistributions.Any(y => y.paymentdistributionid == x.transactionid)).ToList();

1 Comment

thanks for the help dude, i have to mark gilad's answer, because he's the first. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.