【发布时间】:2020-01-24 15:57:31
【问题描述】:
这是我正在尝试做的一个截断示例:
var stuffTOSave = new List<SomeObject> {
public int OtherTableId { get; set; }
public List<Guid> ComponentIds { get; set; }
};
var sql = @"CREATE TABLE Components( ComponentId uniqueidentifier PRIMARY KEY )
INSERT INTO Components VALUES (@WhatGoesHere?)
SELECT * FROM OtherTable ot
JOIN Components c on c.ComponentId = ot.ComponentId
WHERE Id = @OtherTableId
DROP TABLE Components"
Connection.Execute(sql, stuffToSave);
我从other SO questions 知道,您可以使用 Dapper 将列表传递到插入语句中,但我找不到任何传递列表和另一个参数的示例(在我的示例中,OtherTableId),或者具有非对象列表(List<Guid> 与 List<SomeObject> 具有要引用的名称的属性相对)。
对于第二个问题,我可以将ComponentIds 选择到一个列表中,并为其命名:
stuffToSave.ComponentIds.Select(c => new { ComponentId = c })
但是我不确定在我的 sql 查询中放入什么,以便 dapper 了解从我的 ComponentIds 列表中获取 ComponentId 属性(第 7 行)
【问题讨论】: