简体   繁体   English

将另一个列表中的属性添加到实体

[英]add properties from another list to an entity

I have spent hours to find the answers on SO. 我花了几个小时来找到关于SO的答案。 I either don't know how to search or maybe I am asking the question wrong. 我或者不知道如何搜索,或者我问的问题不对。

Here is the situation: 情况如下:

I have an entity Customers. 我有一个实体客户。 I get this from my db context like db.Customers. 我是从db.Customers之类的数据库上下文中获得的。 I have users. 我有用户。 Those are not in my own database but in the database of the CMS. 这些不在我自己的数据库中,而是在CMS的数据库中。 I can retrieve all the users using a thing like UserManager.GetUsers which will give me a IQueryable of the User object (same User object as in System.Web.Security. 我可以使用UserManager.GetUsers之类的东西来检索所有用户,这将为我提供User对象的IQueryable(与System.Web.Security中的User对象相同)。

Now I want to create one list containing all the customers, but I want in that same list also the properties from the User object like IsLockedOut and LastActivityDate. 现在,我想创建一个包含所有客户的列表,但我希望在同一列表中还包含User对象的属性,例如IsLockedOut和LastActivityDate。

I thought it would be simple by creating a new object that has a property Customer and then the other properties. 我认为,通过创建一个具有属性Customer和其他属性的新对象将很简单。 But everytime I put something in the Linq query as GetUser or IsUserLockedOut I get compile errors like "LINQ to Entities does not recognize the method" or "Only parameterless constructors and initializers are allowed". 但是,每次我在Linq查询中将某些内容作为GetUser或IsUserLockedOut时,都会出现诸如“ LINQ to Entities无法识别该方法”或“仅允许无参数构造函数和初始化程序”之类的编译错误。

Is there any way in getting this to work? 有什么办法可以使它起作用?

You can't simply add properties to a concrete type using a Linq query - you can certainly create an anonymous type that has a Customer property and whatever other fields you want to add to the anonymous type. 您不能简单地使用Linq查询将属性添加到具体类型-您当然可以创建具有Customer属性以及要添加到匿名类型的其他任何字段的匿名类型。

The error you are getting is because you are embedding a custom method call that can't be directly translated to SQL into your Linq query somewhere. 您收到的错误是因为您将无法直接转换为SQL的自定义方法调用嵌入到您的Linq查询中的某个位置。 Post your Linq query and it will probably be easy to spot. 发布您的Linq查询,可能很容易发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM