LINQ

M

MrEd

Hi all

I have a class that represents Accounts and each one of them has a
property that has a list of tags (Tag is also a class) so i can do
something like

foreach (Tag currentTag in myAccount.Tags)
Console.WriteLn(currentTag);

no problems there, i want my users to be able to search Accounts by
Tags so if I have a filter like:

List<Tag> filter = new List<Tag>
filter.Add(aTag1);
filter.Add(aTag2);

how can I express a LINQ query that will return the accounts that have
this combination of tags?
 
J

Jon Skeet [C# MVP]

I have a class that represents Accounts and each one of them has a
property that has a list of tags (Tag is also a class) so i can do
something like

foreach (Tag currentTag in myAccount.Tags)
Console.WriteLn(currentTag);

no problems there, i want my users to be able to search Accounts by
Tags so if I have a filter like:

List<Tag> filter = new List<Tag>
filter.Add(aTag1);
filter.Add(aTag2);

how can I express a LINQ query that will return the accounts that have
this combination of tags?

Is this LINQ to Objects? If so:

foreach (Account account in accounts.Where(acc => !
(acc.Tags.Except(filter).Any())))
{
...
}

I wouldn't like to claim it's hugely efficient, but it may well be
good enough...

Jon
 
M

MrEd

Is this LINQ to Objects? If so:

foreach (Account account in accounts.Where(acc => !
(acc.Tags.Except(filter).Any())))
{
...

}

I wouldn't like to claim it's hugely efficient, but it may well be
good enough...

Jon

Thank you Jon for your reply

it is actually LINQ to SQL but if i can get the syntax right and it
works, then later i can worry about efficiency. I'lll give this a go
and let you know.
 
J

Jon Skeet [C# MVP]

it is actually LINQ to SQL but if i can get the syntax right and it
works, then later i can worry about efficiency. I'lll give this a go
and let you know.

It's just possible that the same syntax will indeed work with LINQ to
SQL - I've certainly seen that cope with
someList.Contains(databaseData) before now. Goodness knows what SQL it
would generate, mind you...

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq. Select 2
List to CSV 3
Linq Query. Please, help. Going crazy ... 1
List 2
Contains Error 1
XML and SQL database 3
Linq. String 5
Linq.How to complete this query? 2

Top