Code
Code

Reputation: 2129

Using contains via LINQ query

I will preface with I am incredibly new to LINQ. I am trying to do a simple contains via my LINQ query. Below is my query:

myList.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>().Select(
     x => new ListDetails
          {
             Id = val,
             Name = x.Name
          }).Take(400));

Right now I am using Top 400, but would actually only like to add objects to my list that contain #. Can anyone point me in the right direction on how to achieve this?

Upvotes: 1

Views: 190

Answers (3)

johnny 5
johnny 5

Reputation: 21033

I think you just want to check where the name contains # like you said

myList.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>()
      .Where( x => x.Name.Contains("#").Select(
          x => new ListDetails
          {
              Id = val,
              Name = x.Name
          })));

Upvotes: 4

Roberto
Roberto

Reputation: 2185

This should do it, use contains in where method:

myList.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>()
      .Where(x => x.Name.Contains("#"))
      .Select(
           x => new ListDetails
                 {
                    Id = val,
                    Name = x.Name
                 }));

Upvotes: 4

Jonathan
Jonathan

Reputation: 5028

Something along the lines of

myList.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>()
    .Where(i=>i.Name.Contains("#"))
    .Select(
       x => new ListDetails
             {
                Id = val,
                Name = x.Name
             }).Take(400));

Upvotes: 4

Related Questions