简体   繁体   中英

Entity framework code first - projection problems

I have something like this:

var model = forumsDb.Categories
    .Select(c => new {c, c.Threads.Count})
    .ToList()

My Category object looks like this (pseudocode):

public class Category 
{
     public int id {get;set;}
     public ICollection<Thread> Threads { get; set; }

     /***some properties***/
     [NotMapped]
     public int ThreadCount {get;set;}
}

Now, in my model object, i have two items: model.c and model.Count . How can i map model.Count into model.c.ThreadCount ?

Iterate and assign the value.

foreach(var entry in model)
{
    entry.c.ThreadCount = entry.Count;
}

var categories = model.Select(m => m.c);

Define a strong type:

public class YourModel
{
    public YourModel(Category c, int count)
    {
        C = c;
        Count = count;
        c.Threads.Count = count;
    }

    public Category C { get; set; }
    public int Count { get; set; }
}

var model = forumsDb.Categories
    .Select(c => new YourModel(c, c.Threads.Count))
    .ToList()

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