1

In my project, I need to return a list of car data that does not match the model Id' provided in the array. I'm unsure on how I would go in getting my query to work. So far i have the following below:

var IdList = new List<int> { 60,61,62, 63, 64, 65 };
var query = Context.ManufacturersTable.Where(m => m.Date == date && m.CountryToship = country && m.ExportOnly == false);
if(query.Count() > 0)
    query = query.Where(x => x.CarMoreInfoTable.CarModelTable.Where(f => IdList.Contains(f.ModelId))) //Cannot convert lambda expression to intended delegate type error here

return query

As you can see in the above query I have 3 tables all linked to each other. But please could some direct me on how I would query all data that does not contain the Id's in the given array?

Thank you

4
  • try with query.Where(x => x.CarMoreInfoTable.CarModelTable.Any(f => !IdList.Contains(f.ModelId))), Point of contention use usage of inner Where() Commented Jul 12, 2017 at 9:32
  • 2
    if I've understood this correctly, could you do f => !IdList.Contains(f.ModelId)? Commented Jul 12, 2017 at 9:33
  • Only just noticed that you put an error message in a comment at the end of a line that is out of view. Might be better to put that message in the body of the post so it can be seen without scroll bars. Commented Jul 12, 2017 at 9:38
  • Could you please describe the cardinality of the relationships? Commented Jul 12, 2017 at 9:49

1 Answer 1

2

You have an error because second 'Where' is the issue here, first Where expects to delegate method return bool instead of IEnumerable. (Comment edit) And I think you have to negate:

IdList.Contains(f.ModelId)

Edit

If you need Car Data (I will assume that it is inside CarModel class) then you need change first Where to SelectMany (we would like to merge many collections into one) and negate the inner expression (with contains).

var result = query
.SelectMany(x => x.CarMoreInfoTable.CarModelTable
                  .Where(f => !IdList.Contains(f.ModelId))).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

won't that omit all CarModelTable items even if only one of them is in IdList
@DaveBecker I Edited my answer

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.