0

我有这样的事情:

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

我的 Category 对象如下所示(伪代码):

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

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

现在,在我的模型对象中,我有两个项目:model.cmodel.Count. 我怎样才能映射model.Countmodel.c.ThreadCount

4

2 回答 2

1

迭代并赋值。

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

var categories = model.Select(m => m.c);
于 2012-09-13T15:45:01.707 回答
1

定义一个强类型:

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()
于 2012-09-13T16:00:13.980 回答