在C#属性中存储表值。 [英] Store Table Values In C# Properties.

查看:106
本文介绍了在C#属性中存储表值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Linq在C#属性中存储表值。在三层架构中使用GetAll()方法。不使用Iqueriable方法。

属性只是获取和设置方法。



我想使用Linq和Entity Framework将我的表数据存储在get&set属性中,并且不使用Iqueriable方法。

解决方案

首先,您是否知道可以在DataTable.Rows或DataTable.AsEnumerable()上使用Linq?



我将您的问题解释为表示您不在完全理解.NET中的属性。它们是一个强类型的独立实体,它封装了一个名称/值的键值对,并支持两种特定的内部方法:'set,'get。



虽然很容易将给定类中的所有属性视为Property-Bag,但这也浪费时间,因为你无法对做任何事情将类属性设置为实体 ......没有任何内容可供您使用运行Linq查询!



但是......是的,您可以在运行时使用反射来检查,甚至设置类中的属性的值 instance :这样做会消耗可能影响应用程序性能的资源(内存/计算)。你是否可以在那里使用Linq ......我不确定。



如果你想要一个Property-Bag,有一些选择:



1.如果你能负担得起,或者愿意处理后期绑定:你可以在System.Dynamic中使用Expando对象;例如:



//必需:使用System.Dynamic;

public ExpandoObject MyPropertyBagExpando = new ExpandoObject();



或者,你可以使用字典< string,dynamic>:



public Dictionary< string,dynamic> MyPropertyBagDictionary = new Dictionary< string,dynamic>();



2.如果您的所有属性属于同一类型,则可以使用一个强类型字典,或者为每个不同类型使用多个强类型字典。 br />


无论你使用的是Expando还是Dictionary,你都可以使用Linq;但是,请记住,您通常不会使用Linq来执行具有副作用的代码,例如赋值操作。



映射某种DataTable的Property-Bag的内容需要某种转换,使用XSLT可以使用XML。如果你真的需要一些DataTable内容和一些.NET集合数据结构之间的双向绑定......好吧,有些人可能会使用序列化/反序列化(因为通常需要序列化/反序列化)。

How to Store Table Values In C# properties using Linq. Using GetAll() method in three tier architecture.Without using Iqueriable method.
Properties are just "Get and Set" methods nothing more.

"I want to store my table data in "get& set" properties using Linq, and Entity Framework,
and without using "Iqueriable" method.

解决方案

First, are you aware you can use Linq on DataTable.Rows or DataTable.AsEnumerable() ?

I interpret your question as indicating you don't fully understand what Properties are in .NET. They are a strongly-typed distinct entity which encapsulates a Key-Value Pair of a Name/Value, and supports two specific internal methods: 'set, and 'get.

While it is tempting to see all the Properties in a given Class as a "Property-Bag;" that's also a waste of time because you can't do anything with the set of Class Properties as an "entity" ... There's nothing there for you to run a Linq query on !

However ... yes, you could use reflection at run-time to examine, even set the values of, the Properties in a Class instance: in doing so you will consume resources (memory/computation) that could affect your application's performance. Whether or not you could use Linq there ... I am not sure.

If you want a Property-Bag, there are some choices:

1. if you can "afford," or, are willing to deal with, late-binding: you can use the Expando Object in System.Dynamic; for example:

// required: using System.Dynamic;
public ExpandoObject MyPropertyBagExpando = new ExpandoObject();

or, you could use a Dictionary<string, dynamic>:

public Dictionary<string,dynamic> MyPropertyBagDictionary = new Dictionary<string,dynamic>();

2. if all of your properties are of the same Type, you could use one strongly typed Dictionary, or, use multiple strongly typed Dictionaries for each different Type.

Whether you use an Expando, or Dictionary, you can certainly use Linq with those; however, keep in mind that you do not normally use Linq to execute code with "side-efects," like assignment operations.

To "map" some kind of DataTable's contents to a Property-Bag calls for some kind of "transformation," the kind of thing you can with XML using an XSLT. If you really need two-way binding between some DataTable contents and some .NET collection data-structure ... well, some people might use serialization/de-serialization (since there's often a need to serialize/de-serialize).


这篇关于在C#属性中存储表值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆