简体   繁体   English

什么是 sql 匿名对象,我将如何使用它? 它出现在我正在关注的在线教程中

[英]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.

相关问题 如何将匿名结果类型用于 LINQ 到 SQL 的表达式委托? - How would I use an anonymous result type for an expression delegate for LINQ to SQL? 匿名 Generics - 我将在哪里使用它? - Anonymous Generics - Where would I use this? 我如何将以下T-SQL语句转换为Linq - How would I translate following T-SQL statement To Linq 我如何将以下 JSON 布局表示为 C# object? - How would I represent the following JSON layout as a C# object? 我正在关注 Unity 路径跟踪教程,但在 Int 类型上使用 .Select 时出现此错误,但我不知道该行是做什么的 - I'm following a Unity path tracing tutorial and I'm getting this error about using .Select on an Int type but I have no idea what the line does 我如何将 2 个 sql 命令与 1 个阅读器一起使用? - How would i use 2 sql commands with 1 reader? 我正在尝试为学校作业写一些(OOP),但我遇到了一些问题 - I'm trying to write some (OOP) for a school assignment and I came up with some problems 我如何检查网站a是否在线 - How would I check to see if website a is online 我会用什么Enum? - What would I use an Enum for? 我该如何加快与SQL在线数据库的连接(“配对”速度,而不是查询速度)? - What can i do to speed up the connection (“pairing” speed, not query speed) to my SQL online database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM