0

I need to pass in ids to a webapi controller and return the result. I am currently passing in the ids as a array of objects. I need to match the ids with a dataset and return the result. I found a couple of posts that I am trying to use.

 return db.markets.Where(x => db.markets.Contains(x.x_market_code));

this does not work because the it is not a string or list? I have tried to convert the array to a string or list and i have not got anywhere.

here is code

 public class marketIds
{
    public int x_market_code { get; set; }
}

    [HttpPost]
    [Route("getAssignedMarkets")]
    public IQueryable GetAssignedMarkets(marketIds[] arry)
    {
      return db.markets.Where(x => arry.Contains(x.x_market_code));
    }

I can change how i pass the ids in javascript if need be.

error message image

2
  • What about .ToList(); at the end of your code?? Commented Dec 25, 2015 at 16:51
  • nope didnt do anything Commented Dec 25, 2015 at 16:53

2 Answers 2

2

I assume market code is int so:

[HttpPost]
[Route("getAssignedMarkets")]
public IQueryable GetAssignedMarkets(int[] arry)
{
  return db.markets.Where(x => arry.Contains(x.x_market_code));
}

but you can use string or whatever, however you can still use your class:

 public class marketIds
{
    public int x_market_code { get; set; }
}

    [HttpPost]
    [Route("getAssignedMarkets")]
    public IQueryable GetAssignedMarkets(marketIds[] arry)
    {
      return db.markets.Where(x => arry.where(t=>t.x_market_code == x.x_market_code).Any());
    }

based on your error message , maybe this will help you:

[HttpPost]
[Route("getAssignedMarkets")]
public IQueryable GetAssignedMarkets(int[] arry)
{
  return db.markets.Where(x => arry.Contains(x.x_market_code.Value)).ToList();
}
Sign up to request clarification or add additional context in comments.

1 Comment

the watch is "Enumeration yielded no results"
1

You can convert read all the market_codes from the array to a collection of strings and then use Contains() method in your LINQ query.

var codeList = arry.Select(s => s.x_market_code).ToList();
var result=db.markets.Where(x => codeList.Contains(x.x_market_code));
return result;

Assuming you are comparing it against the market_code column in your market table and it is of type Int.

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.