如何在另一个表达式中创建一个表达式?
如果已经有人问过这个问题,请原谅我。我刚刚开始使用 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
});
}
更新:
这就是我需要做的:
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
}
}
});
但我会在大约 10-15 个不同的类中重复使用 Person()
创建,因此我不希望完全相同的代码重复 X 次。我可能也想为 Company
做同样的事情。
Forgive me if this has been asked already. I've only just started using LINQ. I have the following Expression:
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
});
}
I want to be able to populate LastSalesContact
, which is a Person
entity.
The details that I wish to populate come from m.LatestPerson
, so how can I map over the fields from m.LatestPerson
to LastSalesContact
. I want the mapping to be re-useable, i.e. I do not want to do this:
LastSalesContact = new Person()
{
// Etc
}
Can I use a static Expression, such as this:
public static Expression<Func<TblUser, User>> SelectToUser()
{
return x => (new User()
{
// Populate
});
}
UPDATE:
This is what I need to do:
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
}
}
});
But I will be re-using the Person()
creation in about 10-15 different classes, so I don't want exactly the same code duplicated X amount of times. I'd probably also want to do the same for Company
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能只使用 automapper 吗?
您必须进行一些引导,但它非常可重用。
更新:
我可能没有得到什么,但是这个函数的目的是什么?如果您只想将一个或 Tbl 对象集合映射到其他对象,为什么要有表达式?
你可以这样说:
或者我错过了什么?
Can't you just use automapper for that?
You'd have to do some bootstrapping, but then it's very reusable.
UPDATE:
I may not be getting something, but what it the purpose of this function? If you just want to map one or collection of Tbl object to other objects, why have the expression?
You could just have something like this:
Or is there something I missed?
我不认为您能够使用 lambda 表达式来执行此操作...您需要使用
Expression
中的工厂方法。老实说,这不太可能令人愉快。我通常首选的构建表达式树的方法是从一个简单的示例开始,将您想要执行的操作编写为 lambda 表达式,然后对其进行反编译。这应该向您展示表达式树是如何构建的 - 尽管 C# 编译器比我们更容易使用与属性关联的元数据(我们必须使用 Type.GetProperty)。
这总是假设我已经正确理解了你的意思……很可能我没有正确理解。
I don't think you'll be able to use a lambda expression to do this... you'll need to build up the expression tree by hand using the factory methods in
Expression
. It's unlikely to be pleasant, to be honest.My generally preferred way of working out how to build up expression trees is to start with a simple example of what you want to do written as a lambda expression, and then decompile it. That should show you how the expression tree is built - although the C# compiler gets to use the metadata associated with properties more easily than we can (we have to use
Type.GetProperty
).This is always assuming I've understood you correctly... it's quite possible that I haven't.
这个怎么样:
How about this: