我在关系数据库中有一个自定义实体,我已经通过域模型映射到CLR.因此,通过使用以下语句,我可以通过域模型上的LINQ查询将实体从我的数据库中提取到内存中,就像这样;
var inspection = (from i in dbContext.New_testinspectionExtensionBases
where i.New_testinspectionId == currentInspection
select i).First();
Run Code Online (Sandbox Code Playgroud)
我需要访问此实体上的属性/字段,我需要能够确定属性/字段名称及其值.我想在内存中循环这些项,并将其名称和值写出到控制台.
我尝试使用这种方法,但无法弄清楚如何纠正语法(我也不确定GetProperties是否使用正确的方法,GetFields由于某种原因没有返回任何内容所以我认为这是要走的路)但它并不重要,因为我需要的是对值的读访问权;
var inspectionReportFields = inspection.GetType().GetProperties();
// I called this inspectionReportfields because the entity properties correspond to
// form/report fields I'm generating from this data.
foreach (var reportField in inspectionReportFields)
{
var value = reportField.GetValue();
Console.WriteLine(reportField.Name);
Console.WriteLine(value);
}
Run Code Online (Sandbox Code Playgroud)
在使用像EF或openaccess这样的域模型时,是否有更简单的方法来获取属性/字段值?如果没有,我会以正确的方式进行吗?最后,如果是这样,我如何修复值变量声明中的语法?
以下是域模型生成的代码中的一些示例字段/属性,供参考;
private int? _new_systemGauges;
public virtual int? New_systemGauges
{
get
{
return this._new_systemGauges;
}
set
{
this._new_systemGauges = value;
}
}
private int? _new_systemAlarm ;
public virtual …Run Code Online (Sandbox Code Playgroud) 我有一个C#类,我想循环通过属性作为键/值对但不知道如何.
这是我想做的事情:
Foreach (classobjectproperty prop in classobjectname)
{
if (prop.name == "somename")
//do something cool with prop.value
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法返回操作系统属性列表.我想循环遍历属性并对每个属性进行一些处理.所有属性都是字符串
我如何循环对象
C#
// test1 and test2 so you can see a simple example of the properties - although these are not part of the question
String test1 = OS_Result.OSResultStruct.OSBuild;
String test2 = OS_Result.OSResultStruct.OSMajor;
// here is what i would like to be able to do
foreach (string s in OS_Result.OSResultStruct)
{
// get the string and do some work....
string test = s;
//......
}
Run Code Online (Sandbox Code Playgroud) 如何比较两个对象的属性以确定是否有任何属性已更改?我有一个Patient具有一系列属性的对象.我有第二个对象,UpdatedPatient可能有不同的值.目前,我正在为每个属性使用以下内容:
if (exPt.Id!= pt.Id)
{
exPt.Id = pt.Id;
PatientChanged = true;
}
Run Code Online (Sandbox Code Playgroud)
检查完所有属性后,如果PatientChanged标记为true,则更新患者.是的,它有效,但我在质疑这是否是最有效的解决方案.
如何循环对象的属性并获取属性的值.
我有一个对象,有几个属性填充数据.用户通过提供属性的名称来指定他想要查看的属性,我需要在对象中搜索属性并将其值返回给用户.
我怎样才能实现这一目标?
我写了下面的代码来获取属性但无法获得该prop的值:
public object FindObject(object OBJ, string PropName)
{
PropertyInfo[] pi = OBJ.GetType().GetProperties();
for (int i = 0; i < pi.Length; i++)
{
if (pi[i].PropertyType.Name.Contains(PropName))
return pi[i];//pi[i] is the property the user is searching for,
// how can i get its value?
}
return new object();
}
Run Code Online (Sandbox Code Playgroud) 我想在C#中执行类似下面的操作,但我无法弄清楚如何实例化任何容器类型:
foreach (string aOrB in new Tuple("A","B"))
{
fileNames.Add("file" + aOrB + ".png");
}
Run Code Online (Sandbox Code Playgroud)
我知道在周围范围内创建Tuple/Array/Set容器的典型方法.还有更好的东西吗?
我有以下JSON,我无法控制它,因为它来自外部API:
{
"user_id": "something_name",
"devices": {
"": {
"sessions": [
{
"connections": [
{
"ip": "225.225.225.225",
"user_agent": "something",
"last_seen": 1504266816737
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用JSON反序列化器(JSON.Net)将其变为动态对象.
dynamic values = JsonConvert.DeserializeObject<dynamic>(mes);
Run Code Online (Sandbox Code Playgroud)
问题是,其中一个json键是一个空字符串"".现在我似乎无法像这样调用该属性:
values.devices.<the empty string part>.sessions.connections
Run Code Online (Sandbox Code Playgroud)
当其中一个顶键为空时,如何调用它下方的内容?做devices..sessions不起作用.