0

I have 3 tables: main account, activities & trades.

What I want is to return a bool, based on wither or not a main account has any trades associated with it (connect via an account id column in main account table).

I'm able to tie back the main account and it's associated activities with the following linq query:

From ma in context.MainAccounts
From ac in context.Activities.where(ac => ac.AccountId == ma.AccountId
Select new MainAccountDto{

}

However, I'm struggling to workout how I can set a HasTrades column on my dto object.

I'm not wanting any data from the activities table, just if there are any activities. Would someone be able to please point me in the right direction?

1
  • Use navigation property MainAccount.Activities. Don't join manually, as you and the answers (so far) do. Then it's simply HasTrades = ma.Activities.Any(). Commented Feb 12 at 16:31

2 Answers 2

1

You can achieve this by using .Any() in your LINQ query to check if there are any trades associated with a given MainAccount.

Perhaps something like this:

select new MainAccountDto
{
    AccountId = ma.AccountId,
    HasTrades = context.Trades.Any(t => t.AccountId == ma.AccountId)
};
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm understanding you correctly, maybe:

var dto = (from ma in context.MainAccounts
           let hasTrades = (from ac in context.Activities where ac.AccountId == ma.AccountId select ac).Any()
           select new MainAccountDto{ HasTrades = hasTrades }).Tolist();

Comments

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.