Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq query to exclude from a List when a property value of List of different type are equal?

Tags:

c#

join

linq

I have a List of type Fee from which I need to exclude the ones that have an ID that exists in another List of type int.

List<int> ExcludedFeeIDs = new List<int>{1,2,3,4};

List<Fee> MyFees = (from c in ctx.Fees
                    select c).ToList();

Example: List GoodFees = (from f in ctx.Fees where f.FeeID!=One of the IDs in ExcludedFeeIDs);

Help please?

like image 536
Ozzie Perez Avatar asked Sep 18 '09 17:09

Ozzie Perez


People also ask

What is the use of except in LINQ?

In LINQ, the Except method or operator is used to return only the elements from the first collection, which are not present in the second collection.

What is ToList in LINQ?

LINQ ToList() Method In LINQ, the ToList operator takes the element from the given source, and it returns a new List. So, in this case, input would be converted to type List.

Can LINQ query work with array?

Query writing in LINQLINQ allows us to write query against all data whether it comes from array, database, XML etc.


1 Answers

Try this:

var MyFees = from c in ctx.Fees
             where !ExcludedFeeIDs.Contains(c.FeeID)
             select c;
like image 112
Yannick Motton Avatar answered Sep 20 '22 16:09

Yannick Motton