Dale Fraser
Dale Fraser

Reputation: 4758

Entity to Dictionary conversion simplification

So we are doing things a certain way, and im wondering if its overerly complex, its using unit of work and repositories, however to simplify here is what we do without showing all the code.

first we query and return a list

return _db.content.Where(c => (c.page == page).ToList();

then we loop over the list and convert it into a dictionary

Dictionary<string, ContentEntry> contentResult = new Dictionary<string, ContentEntry>();
foreach (content c in defaultContentQ)
{
    contentResult.Add(c.name, new ContentEntry()

The ContentEntry is a class just to represent the data again.

the content is really just a key pair value (name and data) Im thinking there must be an easier way to go about this.

Upvotes: 0

Views: 242

Answers (1)

Damith
Damith

Reputation: 63105

 var contentResult = _db.content.Where(c => c.page == page)
     .ToDictionary(o => o.name, o => new ContentEntry());

I'm not sure why you have new ContentEntry(), if you want the same object and key as name

 var contentResult = _db.content.Where(c => c.page == page)
     .ToDictionary(c => c.name, c=>c);

Upvotes: 2

Related Questions