请原谅我,如果这已被问及。我只是刚开始使用LINQ。我有以下表现:如何在另一个表达式中创建表达式?
public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary()
{
return m => (new CustomerSummary()
{
ID = m.ID,
CustomerName = m.CustomerName,
LastSalesContact = // This is a Person entity, no idea how to create it
});
}
我希望能够以填充LastSalesContact
,这是一个Person
实体。
我希望填充的细节来自m.LatestPerson
,那么我如何映射从m.LatestPerson
到LastSalesContact
的字段。我不想被重复使用的映射,即我不想这样做:
LastSalesContact = new Person()
{
// Etc
}
我可以用一个静态的表达,比如这个:
public static Expression<Func<TblUser, User>> SelectToUser()
{
return x => (new User()
{
// Populate
});
}
UPDATE:
这是我需要做的:
return m => (new CustomerSummary()
{
ID = m.ID,
CustomerName = m.CustomerName,
LastSalesContact = new Person()
{
PersonId = m.LatestPerson.PersonId,
PersonName = m.LatestPerson.PersonName,
Company = new Company()
{
CompanyId = m.LatestPerson.Company.CompanyId,
etc
}
}
});
但我会重新使用Person()
创建约10-15个不同的类,所以我不想完全相同的代码重复X次的次数。我可能也想为Company
做同样的事情。
我应该澄清,我使用EntityFramework,所以这可能会抛出一些不支持的方法错误。 – GenericTypeTea 2010-08-03 15:01:46
@GenericTypeTea - 为什么?我一直使用automapper和EF。你能发表一个你将如何使用的例子此方法? – Necros 2010-08-03 18:12:43
不起作用:LINQ to Entities无法识别方法'Domain.Models.CustomerSummary Map [TblCustomer,CustomerSummary](Dal.Model.TblCustomer)'方法,并且此方法无法转换为as撕裂表情。我试图在EF Select中使用它。 – GenericTypeTea 2010-08-03 19:27:09