LINQ

  • Thread starter Thread starter MrEd
  • Start date Start date
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?
 
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
 
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.
 
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
 
Back
Top