1

I'm using the repository with the Unit of work pattern based on:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

Their Get function looks like:

 public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")

Now I want to get a List of rows as an output.

The query will also have a WHERE IN clause like:

SELEcT *
FROM ...
   INNER JOIN ...
WHERE homeId in (select homeId ....)

I have the list of home ids:

List<int> homeIdList = ...

How can I build this query using the Get method above?

Get(x => x.Id == cityId, includeProperties: "CityBlock, CityBlock.Homes")

The above is a query that is similiar to what I want, but I want to limit the Homes to be where their id is in the List homeIdList.

Possible?

1 Answer 1

1

Use List's Contains method which will is translated to sql's in operator. I Suggest that you try to get a list of Homes with their CityBlock and City property included, then you can use linq to get all cities:

var repository = new GenericRepository<Home>();
var homes = repository.Get(home => homeIdList.Contains(home.Id), 
                            includeProperties: "CityBlock, CityBlock.City")
                            .ToList();
var cities = homes.Select(h => h.CityBlock.City).Distinct().ToList();

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

but b/c of lazy loading it will fetch the others right? so this won't do what I want...
@user1361315 I'm not sure that I understood you comment, but this GenericRepositoy fetches navigation properties eagerly, thus you don't need lazy loading (that's why we specify includeProperties:...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.