Darth Continent
Darth Continent

Reputation: 2319

Where clause of LINQ statement to find instances of a string within a List<string> collection?

I'm trying to construct a Where clause for a Linq statement which needs to determine whether the AccountNumber values retrieved as below exist in a List<string> collection.

I've thus far tried this:

private void FindAccountNumbers(List<string> AccountNumbers)
{
    var query = from abc 
    select new
    {
        AccountNumber = abc.AccountNumber
    };

    query = query.Where(AccountNumbers.Contains(x => x.AccountNumber));
}

However I get the following build error:

The type arguments for method 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

At runtime, query contains AccountNumber values, and I'm trying to pare this down based on matches found in the AccountNumbers collection (similar to an IN statement in TSQL). Should I be using Intersect instead of Contains? What am I doing wrong??

Upvotes: 1

Views: 3835

Answers (6)

user3394188
user3394188

Reputation: 11

The last answer are wrong because did mention one important point and obviously didn't be tested, the first issue is, you can't mix between an sql query than not been execute and a string's list, YOU CAN'T MIX!!! the solution for this problem and tested is:

var AccountNumbers = select accountNumber from table where.... // is a entitie
    private void FindAccountNumbers(IQueryable<acounts> AccountNumbers) //entitie object not   string
    {
        var query = from abc 
        select new
        {
            AccountNumber = abc.AccountNumber
        };

        query = query.Join(AccountNumbers, abc => abc.AccountNumber, aco=> aco, (ac, coNum) => cpac);
    }

It really works! is necessary to mention this solution is when you are working with linq and entities framework!

Upvotes: -2

cyrotello
cyrotello

Reputation: 747

I'd go with this:

private void FindAccountNumbers(List<string> AccountNumbers)
{
    // Get a strongly-typed list, instead of an anonymous typed one...
    var query = (from a in abc select a.AccountNumber).AsEnumerable();

    // Grab a quick intersect
    var matched = query.Intersect(AccountNumbers)
}

One liner?

var query = (from a in abc select a.AccountNumber).AsEnumerable().Intersect(AccountNumbers);

Upvotes: 0

Joseph
Joseph

Reputation: 25513

This doesn't work?

var query = from x in abc
            where AccountNumbers.Contains(x.AccountNumber)
            select new { x.AccountNumber };

That would give you back any AccountNumber in that list, unless AccountNumber isn't actually a string. That could be your problem.

Upvotes: 2

Bob Vale
Bob Vale

Reputation: 18474

Its because your syntax for from is wrong, I'm guessing that your collection is abc of items to match against is abc

The correct syntax would be (Version 1)

var query = from x in abc
            select new  { AccountNumber = x.AccountNumber };

query = query.Where(x=>AccountNumbers.Contains(x.AccountNumber));

you don't need to do an anonymous type either as you are just wanting the same field you could just do (Version 2)

var query = from x in abc select x.AccountNumber;
query = query.Where(x=>AccountNumbers.Contains(x));

However you could just slap the Where straight onto your original collection. (Version 3)

var query = abc.Where(x=>AccountNumbers.Contains(x.AccountNumber);

Or if you are just trying to find whether any exist in the collection (Version 4)

var query = abc.Any(x=>AccountNumbers.Countains(x.AccountNumber);
  • Version 1 will return IEnumerable<string>
  • Version 2 will return IEnumerable<string>
  • Version 3 will return IEnumerable<type of the items in abc>
  • Version 4 will return bool

Upvotes: 1

BishopRook
BishopRook

Reputation: 1260

Let me verify what you're trying to do.

You have a collection of objects abc. You want to pull out the AccountNumber from each member of that collection, compare it to the list of account numbers passed in, and determine... what? If there IS any overlap, or WHAT the overlap is?

If the AccountNumber field is a string, you could do this:

private IEnumerable<string> OverlappingAccountNumbers(IEnumerable<string> accountNumbers)
{
    return abc.Select(x => x.AccountNumber)
              .Intersect(accountNumbers);
}

Or for the boolean case:

private bool AnyOverlappingAccountNumbers(IEnumerable<string> accountNumbers)
{
    return abc.Select(x => x.AccountNumber)
              .Intersect(accountNumbers)
              .Count() > 0;
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

I think you want to have this:

query = query.Where(x => AccountNumbers.Contains(x.AccountNumber));

Upvotes: 8

Related Questions