LINQ “Where” condition -> change value of property

I have a list of meetings inside which I have another list of attendees.

Model similar to this –

public class Meeting
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public List Users { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
    }

I have list of Meeting

List meetings = GetMeetings();

Now I want to mask the Title of the meetings where one of the users is bot@domain.com. I can achieve this in multiple LINQ queries but I am looking for optimized LINQ query..

Can anyone help me with this?

What I tried –

var maskedMeetings = meetings.Where(x = x.Users.Any(a => a.Email.Equals("bot@domain.com"))); 

meetings = appointments.Except(maskedMeetings).ToList();

maskedMeetings = maskedMeetings.Select(x => { x.Title = "Bot"; return x; }).ToList();

meetings = meetings.Concat(maskedMeetings).ToList();

Can anyone help me with optimized way of writing this query?

Rahul.

How can I write a LINQ query which will coalesce column values in a DataTable into a comma delimited string?

I have a DataTable in a VB.Net application.

I would like to come up with a LINQ expression which would allow me
for a give column, take that column value from each row in the DataTable
and coalesce it into a comma separated string.

Consider a DataTable with rows that look like:

RowID  Value
============
1      A
2      B
3      C
4      D
5      E
6      F

The desire result “A,B,C,D,E,F”

I know I could iterate over the rows to create this result.
My question is,

Can I do this with a simple LINQ query?

Thanks,
JohnB

LINQ's Distinct() on a particular property

I am playing with LINQ to learn about it, but I can’t figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). What I if want to use Distinct on a list of an Object on one or more properties of the object?

Example: If an object is Person, with Property Id. How can I get all Person and use Distinct on them with the property Id of the object?

Person1: Id=1, Name="Test1"
Person2: Id=1, Name="Test1"
Person3: Id=2, Name="Test2"

How can I get just Person1 and Person3? Is that possible?

If it’s not possible with LINQ, what would be the best way to have a list of Person depending on some of its properties in .NET 3.5?

Using LINQ to remove elements from a List

Say that I have LINQ query such as:

var authors = from x in authorsList
              where x.firstname == "Bob"
              select x;

Given that authorsList is of type List, how can I delete the Author elements from authorsList that are returned by the query into authors?

Or, put another way, how can I delete all of the firstname’s equalling Bob from authorsList?

Note: This is a simplified example for the purposes of the question.

remove object from the list if certain statement is true [duplicate]

This question already has an answer here:

using linq I want to check certain condition, if that condition is met I want to remove that object from the list

pseudo code

if any object inside cars list has Manufacturer.CarFormat != null
delete that object

if (muObj.Cars.Any(x => x.Manufacturer.CarFormat != null))
{
    ?
}

Lambda or LinQ expression to remove certain objects from List

I have a cart.Lines List and want to remove all items where quantity == 0

This is a list that holds collection of CartLine objects:

public class Cart
{
    private IList lines = new List();
    public IList Lines { get { return lines; } set { lines = value; } }
}   

public class CartLine
{
    Product Product {get; set;}
    int Quantity {get; set;}

}

So something like:

cart.Lines.RemoveAll(x => x.Quantity == 0)

I only get Remove and RemoveAt, not RemoveAll !

Also can’t remove in a foreach loop, get error:
Collection was modified; enumeration operation may not execute.

I have now managed to do it with this code, surely there must be something more efficient ?

var myList = cart.Lines.ToList();
myList.RemoveAll(x => x.Quantity == 0);
cart.Lines = myList;

Okay Problem solved!Thanks guys, this here does it:

cart.Lines = cart.Lines.Where(x => x.Quantity != 0);

Remove items in one IList<> from another IList<> [duplicate]

This question already has an answer here:

Sorry to post such basic questions, I’m new to LINQ and am trying to figure out the best way to do this short of looping through each IList.

I have 2 ILists<> with of custom dto objects. I want to remove all the matching items from on list that are in the other.

IList list1 = itemsbl.GetBestItems();
IList list2 = itemsbl.GetWorstItems();

I need to remove all the items in list1 from list2. I have been looking at the Except() method but apparently i need my ItemsDTO class to override the GetHashCode and Equals methods for that to work, but i am having trouble finding some examples of this.

Could someone please show me the best way to remove list1 from list2?

Thanks again

Using LINQ to remove elements from a List

Say that I have LINQ query such as:

var authors = from x in authorsList
              where x.firstname == "Bob"
              select x;

Given that authorsList is of type List, how can I delete the Author elements from authorsList that are returned by the query into authors?

Or, put another way, how can I delete all of the firstname’s equalling Bob from authorsList?

Note: This is a simplified example for the purposes of the question.

Removing items in List using LINQ [duplicate]

This question already has an answer here:

I have a List of type some entity

List

public class OrderLine
{
   public string productCode;
   public int quantity;


}

i need to remove items from the above List if the productCode is equal to some products.

List ProductsToBeExcluded = new List(){"1234","1237"};

so, from List i need to remove products which are equal to 1234 and 1237

i have tried

  1. create a List from List using

      List OrderLines = GetOrderLines();
      var ol = from o in OrderLines
            select o.ProductCode;
    

2.

  List ProductsToBeExcluded = new List(){"1234","1237"};
   var filtered = OrderLines.Except(ProductsToBeExcluded);

how do I proceed further in removing

thanks

Remove an item from a LINQ collection

I think I am putting my question correctly 🙂

I am querying the database using LINQ to retrieve some data. Please find the code below.

var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value);

foreach(LeadTypeTaskType l in leadtasktype){
    if (l.TaskTypeId == 21)
    {
        //I need to remove an item which has the tasktype id 21
    }
}

As I mentioned in the comments, I need to remove items from the leadtasktype based on my if condition. How do I do this?

Multiple “order by” in LINQ

I have two tables, movies and categories, and I get an ordered list by categoryID first and then by Name.

The movie table has three columns, ID, Name, and CategoryID.
The category table two has columns, ID, and Name.

I tried something like the following, but it didn’t work.

var movies = _db.Movies.OrderBy( m => { m.CategoryID, m.Name })

Using LINQ to remove elements from a List

Say that I have LINQ query such as:

var authors = from x in authorsList
              where x.firstname == "Bob"
              select x;

Given that authorsList is of type List, how can I delete the Author elements from authorsList that are returned by the query into authors?

Or, put another way, how can I delete all of the firstname’s equalling Bob from authorsList?

Note: This is a simplified example for the purposes of the question.

Using LINQ to delete an element from a ObservableCollection Source [duplicate]

Actually, it is a closer duplicate of:
RemoveAll for ObservableCollections?

Possible Duplicate:
using LINQ to remove objects within a List

This is the code I’m using, but its not very readable. Can I use LINQ to shorten the code below while still having the same functionality?

int index = 0;
int pos = 0;

foreach (var x in HomeViewModel.RecentPatients)
{
   if (x.PID == p.PID)
       pos = index;
   else
       index++;

}

HomeViewModel.RecentPatients.RemoveAt(pos);