1

I got an error message when I tried to query array of IDs in controller action. Here is the error message: string[] does not contain a definition for "contains" and the best extension method overload 'Queryable.contains, Guid)' requires a receiver of type ''. My code is:

public ActionResult GetNews(string[] IDs)
{
    using (DataContext db = new DataContext())
    {  
        var qry = db.News.Where(x => IDs.Contains(x.ID)).ToList();
        ...
    }
}

There is a red line under IDs. IDs is an array passed from View. ID is Guid datatype. Please advice. Thanks.

3
  • 1
    Why don't you just pass an Guid[] to the action method instead of string[]? Commented May 31, 2018 at 17:53
  • I would not expect a string[] to contain a Guid. Commented May 31, 2018 at 17:54
  • It work work with an actual list of IDs instead of your string array: GetNews(string[] IDs) would become GetNews(List<int> IDs)... Commented May 31, 2018 at 18:02

2 Answers 2

1

You need to set id in one list then after you use it same as my code

public ActionResult GetNewsByID(List<int> IDs)
 {
    using (DataContext db = new DataContext())
    {     
        var news= db.News.Where(x => IDs.Contains(x.ID)).ToList();
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Duh, it works when I change the string[] to Guid[]. Thanks for the responses.

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.