LINQ query for removing all objects with an id not equal to a list of ids now workign properly

c# linq select distinct objects from list
c# get distinct values from list of objects
get distinct values from list c# using lambda
c# remove duplicates from list of objects
list<string distinct c#
linq get list of ids from list of objects
c# list distinct multiple columns
entity framework select where id in list

In the var workrolesusersdetailsQuery after filtering I get a list of three int? ids. After that in the foreach loop I am simply trying to remove all the objects with id's not equal to the above mentioned id's. As a result it removes all the objects. The interesting part is that if i replace the != with == then it is working properly.

I just don't see what am i doing wrong, so, please, can somebody help?

var workrolesusersdetailsQuery = db.WorkRolesUsersDetails
            .Where(m => m.UserDetailsId == currentUserId && m.isActive == true)
            .Select(m => m.WorkRoleId).ToList();//select only thw workroleid column

        foreach (var wr in workrolesusersdetailsQuery)
        {
            var temp = wr;
            WorkRolesQuery.RemoveAll((x) => x.WorkRoleId != wr);

        }

Without Foreach use this

WorkRolesQuery.RemoveAll((x) => !workrolesusersdetailsQuery.Contains(x.WorkRoleId));

var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));. Warning: As noted ID));. This variant does not remove duplicates. for Lists. Now for tables with indexed IDs, this would definitely be the way to go. ToList<​string>(); List<string> inBoth = (from o in list1 join p in list2 on o equals p into t from od in t. In the var workrolesusersdetailsQuery after filtering I get a list of three int? ids. After that in the foreach loop I am simply trying to remove all the objects with id's not equal to the above mentioned id's. As a result it removes all the objects. The interesting part is that if i replace the != with == then it is working properly.

I think your delete may also be done using EntityFramework.Extended:

WorkRolesQuery
    .Where(x => !workrolesusersdetailsQuery.Contains(x.WorkRoleId));
    .Delete();

The main advantage over provided solution is that, a single DELETE statement will be issued against the database. For a large amount of deleted items, gained performance is large.

However, a TransactionScope is needed if this delete should be part of a transaction.

In a real scenario, you may want to query your database and load the result to your model. Now let's Now let's try to get the distinct row values from the list using the LINQ Distinct function. This means that the Distinct LINQ function doesn't work at all. public bool Equals(Product x, Product y) { if (Object. Or you might want to remove all instances where the objects match. As per the comment we’re only able to do this as the lists we’re working with contain the same object instances as opposed to different object instances containing the same data. Be careful if you’re trying this as it may trip you up.

foreach (var item in workrolesusersdetailsQuery.Where(x => !WorkRolesQuery.Any(y => y.ID == x.ID)).ToList())
{
enter code here
}

same work you also can do with Contains

secondly Where is give you new list of values. you can achieve your target from Any and contains like Hope this will clear your confusion.

Sounds like you want something like: var query = items.Where(item => validIds.​Contains(item.Id));. Note that if this is all local (i.e. in-process,  The code uses the Contains() method to search for a specific ProductID within the collection which ends up creating a WHERE IN type query. You can do the same thing with LINQ: public static Product [] GetProducts( Guid [] prodIDs) { return ( from p in GetProducts() where prodIDs.Contains(p.ProductID) select p).ToArray< Product >(); }

GitHub is home to over 50 million developers working together to host and IN() list queries are not parameterized, causing increased SQL Server The 5 Ids are so that 1-5 values all use the same plan. Id == 9). EF then converts those LINQ expressions into this SQL expression: Equal(keySelector. LINQ for query; not manipulation. you are right, but LINQ is so mighty, that I was hoping for that too ;-) However, List<Tprovides a predicate-based RemoveAll that does what you

Using the Equals method signature that includes a comparisonType every time you In C# 3.0, the addition of Language-Integrated Query (LINQ) to the language to query collections of objects, in reality you still need to know whether or not In those very rare cases, use #pragma warning disable [warning id] around  What I don't know is how, inside this method, to create a list of staff that match the staff ID's in the results of the query. I'm thinking something along the lines of: 1) Select a list of StaffServices where StaffService.AppointmentTypeID== id

To write the LINQ query expressions to specify criteria and select data, we use This brings us back to C# today and lambda expressions, where we can now just write: The Distinct operator extracts all nonduplicate items from the collection or the hash code and the equals value will both correctly identify the instances  In some situations we are in the position to check two conditions in our logic. So now shall we see how to use the multiple where clause in a linq and lambda query. In some situations we are in a position to check two conditions in our logic. So now shall we see how to use the multiple where clause in a linq and lambda query.

Comments
  • How can I use the 'wr' part without the foreach?