LINQ 中具有附加值的完整对象投影

发布于 2024-09-07 02:42:43 字数 862 浏览 3 评论 0原文

是否可以投影对象的每个属性并添加更多属性,而无需专门列出所有属性。例如,

   var projection = from e in context.entities
                    select new QuestionnaireVersionExtended
                    {
                        Id = e.Id,
                        Version = e.Version,
                        CreationDate = e.CreationDate,
                         ... 
                         many more properties
                         ...
                        NumberOfItems = (e.Children.Count())
                    };

我们可以这样

   var projection = from e in context.entities
                    select new QuestionnaireVersionExtended
                    {
                        e,
                        NumberOfItems = (e.Children.Count())
                    };

做:它将从 e 中获取具有相同名称的每个属性,并将“NumberOfItems”属性添加到该属性上?

Is it possible to project every property of an object and add more, without specifically listing them all. For instance, instead of doing this:

   var projection = from e in context.entities
                    select new QuestionnaireVersionExtended
                    {
                        Id = e.Id,
                        Version = e.Version,
                        CreationDate = e.CreationDate,
                         ... 
                         many more properties
                         ...
                        NumberOfItems = (e.Children.Count())
                    };

Can we do something like this:

   var projection = from e in context.entities
                    select new QuestionnaireVersionExtended
                    {
                        e,
                        NumberOfItems = (e.Children.Count())
                    };

Where it will take every property from e with the same name, and add the "NumberOfItems" property onto that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别再吹冷风 2024-09-14 02:42:46

如果您向 QuestionnaireVersionExtended 添加一个构造函数,该构造函数接受您的实体加上 NumberOfItems,则可以直接使用该构造函数:

var projection = from e in context.entities
     select new QuestionnaireVersionExtended(e, NumberOfItems = (e.Children.Count()));

但是,无法告诉编译器“只需显式复制所有属性”。

If you add a constructor to QuestionnaireVersionExtended that takes your entity plus NumberOfItems, you can use the constructor directly:

var projection = from e in context.entities
     select new QuestionnaireVersionExtended(e, NumberOfItems = (e.Children.Count()));

There is, however, no way to tell the compiler "just copy all of the properties across explicitly."

月亮坠入山谷 2024-09-14 02:42:46

有多种方法可以实现这一目标,但它们都将是噩梦。

1.) 重载构造函数并复制其中的所有值(但这正是您想要摆脱的。2

.)使用反射来复制属性(许多不好的副作用,不推荐)

3.)使用装饰图案。看起来您为原始类添加了值,所以我认为这将是使用装饰器的最佳时机。这也使得添加属性时不会丢失它们。它会破坏编译,但是如果被装饰的对象是密封的,那么这个解决方案并不完美。

There are several ways that you could accomplish this but they all are going to be nightmares.

1.) Overload a constructor and copy all of the values there (however that is what you are trying to get away from.

2.) Use reflection to copy the properties over (many bad side effect, not recommended)

3.) USE THE DECORATOR PATTERN. It looks like you added values to the original class so I think this would be the perfect time to use a decorator. This would also make it so that as properties are added they aren't missed. It would break the compile, however this solution isn't perfect if the object being decorated is sealed.

染火枫林 2024-09-14 02:42:45

不,这是不可能的。 LINQ 表达式的 select 子句允许使用生成值的普通 C# 表达式。没有 C# 构造可以像这样通过对象初始值设定项以模板样式方式创建对象。您需要列出属性或使用显式构造函数。

No this is not possible. The select clause of a LINQ expression allows for normal C# expressions which produce a value. There is no C# construct which will create an object via an object initializer in a template style manner like this. You'll need to either list the properties or use an explicit constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文