Michael
Michael

Reputation: 13616

Some misunderstanding in LINQ to entity

I have this linq to entity row:

myentity.CityType.Add(context.Set<SiteCityType>().FindAsync(2));

Where the entity is myentity and SiteCityType are defined entities. and CityType is navigation property defined in myentity entity:

    public virtual ICollection<SiteCityType> CityType { get; set; }

The linq row above works properly but I need that FindAsync method get List:

myentity.CityType.Add(context.Set<SiteCityType>().FindAsync('some list of ints'));

Any idea how can I implement it?

Upvotes: 2

Views: 99

Answers (1)

ramil89
ramil89

Reputation: 851

Try create extension method:

    public static IEnumerable<SiteCityType> FindById(this SiteCityTypeRepository repository, int[] ids)
    {
        var result = from pr in repository.All() 
                     where ids.Contains(pr.ID)
                     select pr;
        return result.ToList();
    }

I suggest "All()" method to return not in-memory objects, for instance, IQueryable. So actual implementation depends on your architecture.

Upvotes: 2

Related Questions