C# 如何基于运行时选择的属性名称从对象获取特定属性
我有一门课,比方说:C# 如何基于运行时选择的属性名称从对象获取特定属性,c#,asp.net-mvc,C#,Asp.net Mvc,我有一门课,比方说: public class Quote { public string City { get; set; } public string State { get; set; } string ZipCode { get; set; } public string HomeValue { get; set; } } 然后,我在一个页面中显示每个属性名称,旁边有一个复选框。 假设选择了City和HomeValue。 现在,当我查询数据库表“Quote”时,我得到了
public class Quote {
public string City { get; set; }
public string State { get; set; }
string ZipCode { get; set; }
public string HomeValue { get; set; }
}
然后,我在一个页面中显示每个属性名称,旁边有一个复选框。
假设选择了City
和HomeValue
。
现在,当我查询数据库表“Quote”时,我得到了其中的所有数据,我需要过滤这些数据,并在页面的结果中仅显示所选属性及其相关值。。。
如何将从数据库中获取的数据中的属性名称与选定的名称进行匹配。。。
上述示例的预期结果为:
City HomeValue
Atlanta 234000
Orlando 435032
//All of the other values but just for `City` and `HomeValue` properties
你的问题需要更清楚。但如果您试图从数据库加载数据并将其映射到
Quote
类的属性,请不要重新发明轮子。使用简洁:
var sql = "SELECT * FROM Quote";
using (var connection = new SqlConnection("..."))
{
var quotes = connection.Query<Quote>(sql).ToList();
// logic to process a List<Quote>
...
}
var sql=“从报价中选择*”;
使用(var connection=newsqlconnection(“…”)
{
var quotes=connection.Query(sql.ToList();
//处理列表的逻辑
...
}
链接:
流行教程:
如果只从数据库中加载某些列,则需要将列名放在一起,而不是“*”,可能需要使用
StringBuilder
。取决于用户选中这些复选框后得到的结果。您可以使用反射来检索属性和it值。检查以下代码段
型号:
public class Quote
{
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string HomeValue { get; set; }
}
public class QuotesModel
{
public List<Dictionary<string, object>> Quotes { get; set; }
public string[] SelectedProps { get; set; } = AllProps;
public static readonly string[] AllProps;
static QuotesModel()
{
AllProps = typeof(Quote).GetProperties().Select(pi => pi.Name).ToArray();
}
}
<table>
<thead>
<tr>
@foreach (var prop in Model.SelectedProps)
{
@:<td>@prop</td>
}
</tr>
</thead>
<tbody>
@foreach (var q in Model.Quotes)
{
<tr>
@foreach (var prop in Model.SelectedProps)
{
<td>@q[prop]</td>
}
</tr>
}
</tbody>
</table>
公共类报价
{
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串ZipCode{get;set;}
公共字符串HomeValue{get;set;}
}
公共类报价模型
{
公共列表引号{get;set;}
公共字符串[]SelectedProps{get;set;}=AllProps;
公共静态只读字符串[]AllProps;
静态QuotesModel()
{
AllProps=typeof(Quote.GetProperties().Select(pi=>pi.Name.ToArray();
}
}
控制器/操作:
...
[HttpGet]
public IActionResult Quotes([FromQuery] IList<string> props = null)
{
if (props == null || !props.Any())
{
props = QuotesModel.AllProps;
}
var quotes = FetchQuotes();
//--apply properties filter
var selectedPropInfo = typeof(Quote).GetProperties().Where(p => props.Contains(p.Name)).ToList();
var filteredQuotes = quotes
.Select(quote => selectedPropInfo.ToDictionary(pi => pi.Name, pi => pi.GetValue(quote)))
.ToList();
var model = new QuotesModel
{
Quotes = filteredQuotes,
SelectedProps = props.ToArray()
};
return View(model);
}
...
。。。
[HttpGet]
公共IActionResult引号([FromQuery]IList props=null)
{
if(props==null | |!props.Any())
{
props=QuotesModel.AllProps;
}
var quotes=FetchQuotes();
//--应用属性过滤器
var selectedPropInfo=typeof(Quote).GetProperties().Where(p=>props.Contains(p.Name)).ToList();
var filteredQuotes=quotes
.Select(quote=>selectedPropInfo.ToDictionary(pi=>pi.Name,pi=>pi.GetValue(quote)))
.ToList();
var模型=新报价模型
{
Quotes=filteredQuotes,
SelectedProps=props.ToArray()
};
返回视图(模型);
}
...
显示过滤对象:
public class Quote
{
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string HomeValue { get; set; }
}
public class QuotesModel
{
public List<Dictionary<string, object>> Quotes { get; set; }
public string[] SelectedProps { get; set; } = AllProps;
public static readonly string[] AllProps;
static QuotesModel()
{
AllProps = typeof(Quote).GetProperties().Select(pi => pi.Name).ToArray();
}
}
<table>
<thead>
<tr>
@foreach (var prop in Model.SelectedProps)
{
@:<td>@prop</td>
}
</tr>
</thead>
<tbody>
@foreach (var q in Model.Quotes)
{
<tr>
@foreach (var prop in Model.SelectedProps)
{
<td>@q[prop]</td>
}
</tr>
}
</tbody>
</table>
@foreach(模型中的var属性。SelectedProps)
{
@:@prop
}
@foreach(Model.Quotes中的var q)
{
@foreach(模型中的var属性。SelectedProps)
{
@q[道具]
}
}
你对这篇文章有什么问题吗?我需要在运行时将选定的属性名称映射到对象属性名称,从数据库中检索数据没有任何问题。请查看Dapper,看看这是否有帮助。不过,我也在使用实体框架,我认为在C部分有一种方法可以做到这一点…@AlexGH,编辑了我的答案。“选定的属性名称”是什么数据类型/格式?