将连接表导出为listView,匿名类型数据

时间:2013-03-08 13:38:59

标签: c# entity-framework join listbox

我加入了3个表,我想将数据导出到ListView,我该怎么做?数据是匿名类型的列表,如何获取值,以便将它们添加到listView?

var results = (from a in db.CUSTOMERs
                       join b in db.ORDERs on a.CUSTOMERID equals b.CUSTOMERID
                       join c in db.ORDERDETAILS on b.ORDERID equals c.ORDERID
                       select new
                       {
                           a.CUSTOMERID,
                           a.NAME,
                           a.ADRESA,
                           b.ORDERID,
                           b.DATA,
                           b.VALOARE,
                           c.PRODUS,
                           valoare2 = c.VALOARE,
                           c.SERIAL
                       }).ToList();

1 个答案:

答案 0 :(得分:0)

您需要定义一个公共类来保存数据

public class modelData
{
    public string CUSTOMERID { get; set; }
    public string NAME { get; set; }
    public string ADRESA { get; set; }
    public string ORDERID { get; set; }
    public string DATA { get; set; }
    public string VALOARE { get; set; }
    public string PRODUS { get; set; }
    public string VALOARE2 { get; set; }
    public string SERIAL { get; set; }
};

var results = (
    from a in db.CUSTOMERs
    join b in db.ORDERs on a.CUSTOMERID equals b.CUSTOMERID
    join c in db.ORDERDETAILS on b.ORDERID equals c.ORDERID
    select new modelData
    {
       CUSTOMERID = a.CUSTOMERID,
       NAME = a.NAME,
       ADRESA = a.ADRESA,
       ORDERID = b.ORDERID,
       DATA = b.DATA,
       VALOARE = b.VALOARE,
       PRODUS = c.PRODUS,
       VALOARE2 = c.VALOARE,
       SERIAL = c.SERIAL
    })
    .ToList();