Daza Aza
Daza Aza

Reputation: 498

combine two queries into one one query

I have two queries which both work but need to combine them into one working query

Query 1

  var now = DateTime.Now;
  var twoWeeksAgo = now.AddDays(-14);

  var advert =
      from m in db.tbl_Property
      where m.Prop_Update >= twoWeeksAgo && m.Prop_Update <= now
      select m;

Query 2

  var Props = 
      from n in db.tbl_Property.
      Where(c => c.Prop_AvailableSpaces > 0)
      select n;

Any help or advice welcome

Upvotes: 1

Views: 377

Answers (4)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

     var advert = db.tbl_Property.Where(m => m.Prop_Update >= twoWeeksAgo 
     && m.Prop_Update <= now && m.Prop_AvailableSpaces > 0);

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064104

I would change those queries to pass in the original IQueryable<Property> (or whatever the type is), then you can do:

var advert = from m in source
             where m.Prop_Update >= twoWeeksAgo && m.Prop_Update <= now
             select m;

passing db.tbl_Property in as the source, then:

var Props = from n in filteredProperties
            where n.Prop_AvailableSpaces > 0
            select n;

i.e. composition

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503449

Do you mean something like:

var now = DateTime.Now;
var twoWeeksAgo = now.AddDays(-14);

var advert = db.tbl_Property
               .Where(m => m.Prop_Update >= twoWeeksAgo && m.Prop_Update <= now
                           && m.Prop_AvailableSpaces > 0);

Or perhaps combining with || instead:

var advert = db.tbl_Property
               .Where(m => (m.Prop_Update >= twoWeeksAgo && m.Prop_Update <= now)
                           || m.Prop_AvailableSpaces > 0);

(Note that I haven't used a query expression here as you're only using a single where clause.)

Upvotes: 2

Arion
Arion

Reputation: 31249

Maybe something like this:

var advert =
            from m in db.tbl_Property
            where 
                (m.Prop_Update >= twoWeeksAgo && m.Prop_Update <= now)
                || (m.Prop_AvailableSpaces > 0)

            select m;

Upvotes: 0

Related Questions