1

我目前有以下 linq,它运行并获取两个强类型对象(DAL.Driver 和 DAL.Licence)。但是,我想将结果转换为包含 BLL.Driver 和 BLL.Licence 对象的单个 DriverODSJoined 对象。

public class DriverODSJoined
{
    public BLL.Driver driver { get; set; }
    public BLL.Licence licence { get; set; }

    public static void GetData()
    {
        DAL.DriverDataContext dataContext = new DAL.DriverDataContext();

        var query = (from d in dataContext.drivers
                     join c in dataContext.licences on d.licence_id equals c.id into t1
                     from t2 in t1.DefaultIfEmpty()
                     select new { Driver = d, Licence = t2 });
    }
}

对于一个类对象的链接查询,我会这样做:

query.Select(a => new BLL.Driver.Driver()
        {
            id = a.Driver.id
            etc
        }).ToList();

因此,要填充 DriverODSJoined 列表,我想我会做这样的事情:

  query.Select(a => new BLL.Driver.DriverODSJoined()
        {
            driver.id = a.Driver.id,
            licence.id = t2.id
        }).ToList();

但是它不起作用。我该如何做才能得到一个 List ,每个 List 都包含一个 BLL.Driver 和 BLL.Licence 对象的实例?

谢谢,理查德

4

1 回答 1

2

不久之后我想通了,当然我必须先实例化包含对象中的每个对象,然后再将值分配给它们的成员变量。所以这里是一个例子:

List<DriverODSJoined> list = query.Select(a => new DriverODSJoined()
{ 
    driver = new Driver()
    { 
        address1 = a.driver.address1
    },
    check = new BLL.DVLA.Licence()
    {
        id = a.licence.id 
    } 
}).ToList();
于 2012-05-24T07:17:48.543 回答