user7629010
user7629010

Reputation:

Multiple Select using LINQ

I have LINQ query like this

ApplicationDbContext context = new ApplicationDbContext();

IEnumerable<InvitationMails> customers = context.InvitationMails
            .Where(c => c.LinkStart == null)
            .AsEnumerable()
            .Select(c => {
                c.LinkStart = start;
                return c;
            });

I need to select LinkStart, LinkEnd, and Link columns with checking for null values like this.

.Where(c => c.LinkStart == null)
.Where(c => c.LinkEnd == null)
.Where(c => c.Link == null)

But how do write query for select?

Model

public string Link { get; set; }
public DateTime? LinkStart { get; set; }
public DateTime? LinkEnd { get; set; }

Upvotes: 0

Views: 8922

Answers (2)

Sнаđошƒаӽ
Sнаđошƒаӽ

Reputation: 17552

LINQ allows you to select any combination of properties you want without having to define a type for every combination of properties. When you don't specify type in your Select, your selections are morphed into an anonymous object. This is how you can do it:

var result = context.InvitationMails
             .Where(c => c.LinkStart == null && c.LinkEnd == null && c.Link == null)
             .Select(c => new { c.LinkStart, c.LinkEnd, c.Link });

result is an anonymous object with the properties specified.

Note that, as long as you are not required to pass the result object to any method, or return result from the current method, you are good to go. However, if you do need to do any of that, you have to create a class (or struct) for that (or, you could make do with object at the expense of type inference/intellisense).

Also note that you don't have to chain the Where one after another, you can simply use boolean operators like && to separate your conditions.

Edit: If your model is composed entirely of those three properties, you don't need a Select at all. You can simply do this:

var result = context.InvitationMails
             .Where(c => c.LinkStart == null && c.LinkEnd == null && c.Link == null);

Note in that case result will not be IQueryable of anonymous types, but of InvitationMail (considering your model's name is InvitationMail).

Upvotes: 4

steliosbl
steliosbl

Reputation: 8921

Since the properties you are trying to select aren't all of the same type, you need to create a struct if you want to keep them all in the same IEnumerable. An IEnumerable<pair<string, pair<DateTime?, DateTime?>>> would also work, but the struct solution seems cleaner.

public struct Customer
{
   string Link;
   DateTime? LinkStart;
   DateTime? LinkEnd;

   public Customer(string link, DateTime? linkStart, DateTime? linkEnd)
   {
       this.Link = link
       this.LinkStart = linkStart;
       this.LinkEnd = linkEnd;
   }
}

Once you have that then you can do:

        .Where(c => c.LinkStart == null)
        .Where(c => c.LinkEnd == null)
        .Where(c => c.Link == null)
        .Select(c => new Customer(c.Link, c.LinkStart, c.LinkEnd));

Upvotes: 0

Related Questions