简体   繁体   中英

Filter list of entity objects by string child object property using Linq Lambda

I am trying to return an IQueryable lands filtered by a child object property Owner.Name. Is working well with the query style solution, but I want to use a lambda one.

On short these are my classes mapped by EntityFramework:

public class Land
{
     public int Id { get; set; }

     public virtual ICollection<Owner> Owners { get; set; }
}

public class Owner
{
        public int Id { get; set; }
        public string Name { get; set; }
        public int LandId { get; set; }

        public virtual Land Lands { get; set; }
}

The query which is working fine:

 var list = from land in db.Lands
                   join owner in db.Owners on land.Id equals Owner.LandId
                   where owner.Name.Contains("Smit")
                   select land;

I was trying using this:

var list = db.Lands.Where(lnd => lnd.Owners.Count() > 0 &&
             lnd.Owners.Where(own => own.Name.Contains("Smit")).Count() > 0);

It works only for small lists, but for some with thousands of records it gives timeout.

Well, one issue which may be causing the speed problem is that your lambda version and your non-lambda versions do very different things. You're non lambda is doing a join with a where on one side of the join.

Why not just write the lambda equivalent of it?

var list = db.Lands.Join(db.Owners.Where(x=> x.Name.Contains("Smit")), a=> a.Id, b => b.LandId, (a,b) => a).toList();

I mean, that is the more direct equivalent of your non lambda

我认为您可以使用以下一种:

var list = db.Lands.Where(lnd => lnd.Owners.Any(x => x.Name.Contains("Smit")));

Try something more straightforward:

var lands = db.Owners.Where(o => o.Name.Contains("Smit")).Select(o => o.Lands);

You just need to make sure that Owner.Name is not null and LINQ will do the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM