2015-07-13 20 views
0

我需要计算嵌套对象列表,但不想返回嵌套对象。返回嵌套对象计数而不列出它们的最佳方法

我有类:

public class Categoria 
{ 
    public int ID {get; set} 
    public List<Produto> produto {get; set;} 
    public int produtoCount {get; set;} 
} 

public class Produto 
{ 
    public ID {get; set} 
    public string data {get; set;} 
} 

我在Categoria类使用produtoCount试过了,但是当我使用一个包含Produto类的,这样它只有值:

(new Categoria()).AsQueryable().Include(p => p.Produto).ToList(); 

是有可能吗?

回答

1

当然这是可能的:

DbContext.Categorias 
    .Where(c => c.Id == id) 
    .Select(c => new  
    { 
    // Assuming you also want the Categoria 
    Categoria = c, 
    ProdutoCount = c.produto.Count() 
    }) 
    .FirstOrDefault(); 
0

请尝试以下代码。它可能会帮助你。

public class Categoria 
{ 
    public int ID {get; set} 
    private List<Produto> produto {get; set;} 
    public int produtoCount {get {return produto.Count();} } 
} 

public class Produto 
{ 
    public ID {get; set} 
    public string data {get; set;} 
} 

感谢, 阿米特生主

+0

这不会解决',但不想返回嵌套对象。正如OP所述。 –

相关问题