B
B
Boris the Animal2015-02-14 18:57:24
Entity Framework
Boris the Animal, 2015-02-14 18:57:24

How to get records from a table based on a collection of keys?

This method is of interest:
DbPicture[] GetImages(string[] keys)

public interface IContentRepository
    {
        void AddImage(DbPicture image);

        void AddImages(DbPicture[] images);

        DbPicture[] GetImages(string[] keys);

        DbPicture GetImage(string key);
    }

    public class ContentRepository : IContentRepository
    {
        private readonly ContentDbContext _context;

        public ContentRepository()
        {
            _context = new ContentDbContext();
        }

        public void AddImage(DbPicture image)
        {
            _context.Pictures.Add(image);
            _context.SaveChangesAsync();
        }

        public void AddImages(DbPicture[] images)
        {
            _context.Pictures.AddRange(images);
            _context.SaveChangesAsync();
        }

        public DbPicture[] GetImages(string[] keys)
        {
            return _context.Pictures. ??????????????????????;
        }

        public DbPicture GetImage(string key)
        {
            return _context.Pictures.Find(key);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2015-02-14
@Casper-SC

I figured it out myself. Decided like this:

public DbPicture[] GetImages(string[] keys)
        {
            return _context.Pictures.Where(p => keys.Any(k => k == p.FileName)).ToArray();
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question