3

Hi all I have a query using Linq that returns a set of records, one of which is the country id, I also have an array of desired countries. Is there a way of either looping through the countries array and seeing if the id is in the results, i want to do something like this

results = from r in results 
where
//jump to my c# array
for(int x = 0;x < array.count; x++)
{
r.countryId.ToString().Contains(array[x]) 
}
select r

thanks

3 Answers 3

4

Try this

var list  =  from r in results
             where array.Contains(r.countryId.ToString()) 
             select r;
Sign up to request clarification or add additional context in comments.

Comments

1

You could join the collections, but I think Yograj Gupta's answer is probably the better one.

var query = from a in results
            join b in array
            on a.CountryId equals b.CountryId
            select a;

Comments

0

You didn't give the array type so assuming it's testclass1

    class TestClass1
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class Country
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    List<Country> countries = new List<Country>();
    TestClass1[] arrays = new TestClass1[30];

    countries.Where(x => arrays.Select(y => y.Id).Contains(x.Id)).ToList();

I'm not sure if this is the best approach but i think this will work.

UPDATE : didn't notjce the array is of type country as well so. sorry.

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.