如何在IList类型中访问IList类型中的项目? [英] How do I access, in a IList Type, the items in a IList type ?

查看:71
本文介绍了如何在IList类型中访问IList类型中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我正在使用EF而且我有两个类:HRecibos和Clientes:

Hi!
I'm using EF and I have two classes: HRecibos and Clientes:

[Table("Clientes")]
	public class Cliente
	{
		[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
		public int     CodCliente   { get; set; }
		public string  TitHonorif   { get; set; }
		public string  Nome         { get; set; }
		public string  Morada       { get; set; }
		public string  CodPostal    { get; set; }
		public string  Localidade   { get; set; }
		public string  NrContrib    { get; set; }
		public string  Obs          { get; set; }
		public decimal Valor        { get; set; }
		public byte    Status       { get; set; }  

        public IEnumerable<hrecibo>; Recibos { get; set; }
	}
    [Table("HRecibos")]
    public class HRecibo
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string  NrRecibo     { get; set; }
        public int     CodCliente   { get; set; }
        public Cliente Cliente { get; set; }
	public decimal Valor        { get; set; }
        public byte    Status       { get; set; }  
    }



我有一个DBContext:


And I've a DBContext:

public partial class CAPDBContext : DbContext
{
    public CAPDBContext()
        : base("CAPDBContext")
    {
        Database.SetInitializer<CAPDBContext>( null );
    }
    public DbSet<hrecibo>; Recibos { get; set; }
    public DbSet<cliente>; Clientes { get; set; }



我想阅读所有Recibos(收据)并将客户名称放在Cliente Code之后。

我写了这个:


I want to read all the "Recibos("Receipts") and put the client's name after the Cliente Code.
I wrote this:

public DataTable GetAll()
{
    using (var context = new CAPDBContext())
    {
        IList<hrecibo> HRecibos = context.Recibos.ToList();
        IList<cliente> Clientes = context.Clientes.ToList();

        return HRecibos.ToDataTable<hrecibo>();
    }
}



所以我的IList类型Hrecibos包含所有Recibos(收据)和其中的另一个Ilist Cliente。

问题是:我如何得到客户端的名称包含客户数据的内幕IList?



提前谢谢
$ b $bAntónioBarros


So I've the IList type "Hrecibos" with all "Recibos" (Receipts) and another Ilist Cliente inside it.
The problem is: How I get the name of the client in the insider IList that contains the clients data?

Thank you in advance
António Barros

推荐答案

最简单的选择是使用navigation属性来读取必要的信息:

The simplest option would be to use the navigation property to read the necessary information:
public IList<HRecibo> GetAll()
{
    using (var context = new CAPDBContext())
    {
        return context.Recibos
            .Include(r => r.Cliente)
            .ToList();
    }
}
...
foreach (HRecibo item in GetAll())
{
    Console.WriteLine("{0}, {1}, {2}", item.NrRecibo, item.CodCliente, item.Cliente.Nome);
}



如果你需要压扁列表,你可以使用匿名类型:


If you need to flatten the list, you can either use an anonymous type:

public IList<dynamic> GetAll()
{
    using (var context = new CAPDBContext())
    {
        return context.Recibos
            .Include(r => r.Cliente)
            .Select(r => new 
            { 
                r.NrRecibo,
                r.CodCliente,
                Nome = r.Cliente.Nome,
                r.Valor,
                r.Status,
            })
            .ToList();
    }
}



或者您可以为数据创建特定类型:


Or you can create a specific type for the data:

public sealed class HReciboDto
{
    public string  NrRecibo     { get; set; }
    public int     CodCliente   { get; set; }
    public string  Nome         { get; set; }
    public decimal Valor        { get; set; }
    public byte    Status       { get; set; }  
}

public IList<HReciboDto> GetAll()
{
    using (var context = new CAPDBContext())
    {
        return context.Recibos
            .Include(r => r.Cliente)
            .Select(r => new HReciboDto
            { 
                NrRecibo = r.NrRecibo,
                CodCliente = r.CodCliente,
                Nome = r.Cliente.Nome,
                Valor = r.Valor,
                Status = r.Status,
            })
            .ToList();
    }
}


这篇关于如何在IList类型中访问IList类型中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆