[英]What is a sql anonymous object and how would i use it? It came up in an online tutorial i'm following
What is a sql anonymous object and how would i use it?什么是 sql 匿名对象,我将如何使用它? It came up in an online tutorial i'm following.
它出现在我正在关注的在线教程中。
Sample code:示例代码:
Sql = "select Id from dbo.contacts where FirstName = @FirstName and LastName = @LastName;";
int contactId = DB.LoadData<IdLookupModel, dynamic>(Sql, new
{
contact.BasicInfo.FirstName,
contact.BasicInfo.LastName
},
_ConnectionString).First().id;
The anonymous object is not part of sql but of your dotnet code.匿名对象不是 sql 的一部分,而是您的 dotnet 代码的一部分。 See https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types for more details.
有关更多详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types 。
In general you can say that you may create any object without a corresponding class.一般来说,您可以说您可以创建任何没有相应类的对象。 In your example this is the anonymous object:
在您的示例中,这是匿名对象:
new {
contact.BasicInfo.FirstName,
contact.BasicInfo.LastName
}
It is passed as the parameters to you sql queries and then mapped to @FirstName
and @LastName
.它作为参数传递给您的 sql 查询,然后映射到
@FirstName
和@LastName
。 The names are taken from the property names.名称取自属性名称。 You can also set your own names:
您还可以设置自己的名称:
new {
GivenName = contact.BasicInfo.FirstName,
FamilyName = contact.BasicInfo.LastName
}
You can than access the parameters via @GivenName
and @FamilyName
.您可以通过
@GivenName
和@FamilyName
访问参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.