字典名saveModel,字段叫OutWareHouseBillList_Id,判断字段是否存在字典里用C#语言编写
时间: 2024-06-02 12:14:03 浏览: 6
语言该怎么写?
在C语言中,可以使用以下代码来判断字典名为saveModel中是否存在字段名为OutWareHouseBillList_Id的字段:
if (saveModel.OutWareHouseBillList_Id) {
printf("字段存在");
} else {
printf("字段不存在");
}
相关问题
C# DAPPER如何用字典获取查询的两个字段
使用Dapper获取查询结果中的两个字段可以使用字典来存储,示例代码如下:
```csharp
using Dapper;
using System.Collections.Generic;
using System.Data.SqlClient;
public class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
}
public static class MyDatabase
{
private static string connectionString = "your_connection_string_here";
public static IEnumerable<Dictionary<string, object>> GetIdsAndNames()
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var query = "SELECT Id, Name FROM MyTable";
var results = connection.Query(query);
return results;
}
}
}
// Usage:
var results = MyDatabase.GetIdsAndNames();
foreach (var result in results)
{
var id = (int)result["Id"];
var name = (string)result["Name"];
// Do something with id and name...
}
```
在上面的示例中,`GetIdsAndNames`方法使用Dapper执行了一条查询语句,返回了一个`IEnumerable<Dictionary<string, object>>`类型的结果集,其中每个字典代表一行查询结果,包含了`Id`和`Name`两个字段的键值对。在使用结果集时,可以通过字典的键来访问对应的值。
要给出通过的方法,用于Object对象转换成Dictionary<string,object>字典对象,key为字段名称,value为字段的值,给出c#代码
可以使用反射来实现Object对象转换成Dictionary<string, object>字典对象,具体代码如下:
```csharp
using System;
using System.Collections.Generic;
using System.Reflection;
public static class ObjectExtensions
{
public static Dictionary<string, object> ToDictionary(this object obj)
{
var dict = new Dictionary<string, object>();
if (obj == null) return dict;
var type = obj.GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var propertyName = property.Name;
var propertyValue = property.GetValue(obj, null);
dict[propertyName] = propertyValue;
}
return dict;
}
}
```
使用示例:
```csharp
var person = new { Name = "Tom", Age = 18 };
var dict = person.ToDictionary();
foreach (var item in dict)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
```
输出结果:
```
Name: Tom
Age: 18
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)