[英]Select only few columns from a table to map with Odata Select query on C# Web API
实体框架示例
从包含列“id”、“col1”、“col2”、“col3”的表中选择列“id”和“col3”。
class filtededMyTableRow
{
public long Id { get; set; }
public string col3 { get; set; }
}
// ControllerName/GetCols?colNames=Id&colNames=col3
public void GetCols(string[] colNames)
{
using (DbEntities context = new DbEntities())
{
/*
MyTable
id col1 col2 col3
1 text1 text2 text3
2 text1 text2 text3
3 text1 text2 text3
4 text1 text2 text3
5 text1 text2 text3
6 text1 text2 text3
*/
string selArg = string.Join(",", colNames);
List<filtededMyTableRow> filtededMyTable = context.Database
.SqlQuery<filtededMyTableRow>($"SELECT {selArg} FROM MyTable")
.ToList();
/*
filtededMyTable
Id col3
1 text3
2 text3
3 text3
4 text3
5 text3
6 text3
*/
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.