- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我尝试执行此方法时出现以下异常:
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id“
public static IEnumerable<FinancePositionList> GetFinancialListsForConsecutiveYears(int year, int periodTypeId, int period)
{
IEnumerable<FinancePositionList> resultList;
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Finance"].ConnectionString))
{
conn.Open();
StringBuilder query = new StringBuilder();
query.Append("SELECT b.CompanyId,b.CompanyName,[Year]");
query.Append(",CreationDate,AccruedExpenses,AdvancePaymentsToContractors");
query.Append("FROM finance.FinanceList a INNER JOIN finance.Company b ");
query.Append("ON a.CompanyId = b.CompanyId ");
query.Append("INNER JOIN finance.ListPeriod c ");
query.Append("ON c.FinanceListId = a.FinanceListId ");
query.Append("WHERE Discriminator = " + "'" + "FinancePositionList" + "' ");
query.Append("AND[Year] IN @years ");
query.Append("AND c.PeriodTypeId = @PeriodTypeId AND c.Period = @Period ");
query.Append("ORDER BY b.CompanyId, a.[Year] DESC ");
resultList = conn.Query<FinancePositionList, Company,ListPeriod, FinancePositionList>(query.ToString(),
(a, b,c) =>
{
a.Company = b;
c.FinanceList = a;
return a;
},
new
{
years = new[] { year, year - 1 },
PeriodTypeId = periodTypeId,
Period = period
},
splitOn: "CompanyId,FinanceListId").AsEnumerable();
}
return resultList;
}
编辑:
我现在通过像这样更改列的顺序来解决问题:但我想知道是否可以对代码进行更多增强?
public static IEnumerable<FinancePositionList> GetFinancialListsForConsecutiveYears(int year, int periodTypeId, int period)
{
IEnumerable<FinancePositionList> resultList;
using (var conn = new SqlConnection(ResolveConnectionString()))
{
conn.Open();
StringBuilder query = new StringBuilder();
query.Append("SELECT CreationDate,AccruedExpenses,AdvancePaymentsToContractors,[Year]");
query.Append(",b.CompanyId,b.CompanyName,c.FinanceListId ");
query.Append("FROM finance.FinanceList a INNER JOIN finance.Company b ");
query.Append("ON a.CompanyId = b.CompanyId ");
query.Append("INNER JOIN finance.ListPeriod c ");
query.Append("ON c.FinanceListId = a.FinanceListId ");
query.Append("WHERE Discriminator = " + "'" + "FinancePositionList" + "' ");
query.Append("AND [Year] IN @years ");
query.Append("AND c.PeriodTypeId = @PeriodTypeId AND c.Period = @Period ");
query.Append("ORDER BY b.CompanyId, a.[Year] DESC ");
resultList = conn.Query<FinancePositionList, Company, ListPeriod, FinancePositionList>(query.ToString(),
(a, b, c) =>
{
a.Company = b;
a.CompanyId = b.CompanyId;
a.FinanceListId = c.FinanceListId;
return a;
},
new
{
years = new[] { year, year - 1 },
PeriodTypeId = periodTypeId,
Period = period
},
splitOn: "CompanyId,FinanceListId").AsEnumerable();
}
return resultList;
}
最佳答案
您大多误解了 Multimapping
的工作原理使用 Dapper
, 您的查询产生以下列:
**"CompanyId","CompanyName","Year","CreationDate","AccruedExpenses",
"AdvancePaymentsToContractors"**
现在在 multimapping
代码,下面是小巧玲珑的Query
您正在调用的重载(从 source code
中检查):
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(
this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TReturn> map,
object param, IDbTransaction transaction, bool buffered, string splitOn,
int? commandTimeout, CommandType? commandType)
早些时候我误解了整个调用,但现在看来,问题仅在于使用 SplitOn 的多映射类型映射,您已经更正了这一点,因此只要正确spliton
查询结果中有可用的列
conn.Query<FinancePositionList, Company,ListPeriod, FinancePositionList>(query.ToString(), (a, b,c) =>
{
a.Company = b;
c.FinanceList = a;
return a;
},
new
{
years = new[] { year, year - 1 },
PeriodTypeId = periodTypeId,
Period = period
}, splitOn: "CompanyId,FinanceListId")
现在只有一点有待澄清,因为我已经在评论中发布了正在运行的 years
参数,主要是 integer array
, 对于当前全是文本的查询,这可以正常工作,但不适用于 stored procedures
,它期望使用 DataTable
的地方相同, 具有与集合相同的列序列和名称只能使用 Table Valued Parameters
提供.我预计当前用例不需要任何更多更改。
编辑以提供匿名参数和动态参数的示例:
匿名参数
这个简单的 C# 匿名类型,检查 here ,想法是您可以使用像 {max = <value>,min=<value>}
这样的简单占位符来提供所有参数或者即使名称匹配 {max,min}
也是如此, 在这两种情况下参数都是 @max
和 @min
,大小写无关紧要,您使用的是 AnonymousParameters
在您的参数代码中 years, PeriodTypeId, Period
, 它将在内部推断出类型和其他细节,并假设所有这些都是输入参数
{years = new[] { year, year - 1 },PeriodTypeId = periodTypeId,Period = period}
动态参数
它们更像是 Ado.Net 中的参数类,它让你显式地添加一个参数,下面是 Dapper 代码中的重载,你必须提供所有信息,如 Type, Direction
等等(来自 dapper 源代码的代码片段):
public partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup, SqlMapper.IParameterCallbacks
{
public void Add(string name, object value, DbType? dbType, ParameterDirection? direction, int? size)
public void Add(string name, object value = null, DbType? dbType = null, ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null)
}
关于c# - 使用多映射 API 时,如果您有 Id“, ”splitOn 以外的键,请确保设置 splitOn 参数与多个关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40276673/
我试图通过屏幕截图捕获带有突出显示的元素,但在屏幕截图中,该元素不带有突出显示。有谁知道解决办法吗?当我捕获文本时会出现突出显示:automatests@gmail.com 查看我的代码: Utili
大家好,我是编码的新手,我正在和一位老师一起上 Java 入门课,希望您已经了解一切。我必须对冰雹序列进行编码,它表示为: 选择一些正整数并将其命名为 n。如果 n 是偶数,则将其除以二。如果 n 是
如果存在名称相同的SCOM组,则尝试制作一个请求更多信息(组ID)的脚本: function myFunction { [CmdletBinding()] Param(
我有这张表: id | CUPNAME | FRENCHNAME 1 | 2 | null 2 | null | 4 我想从非空的 CUPNAME 和 FRENCHNAME 中提
我是 Collection View 的新手,想知道这是否是创建它们的最佳方式,我还想了解一些关于从哪里转到启用分页的详细 View 的建议。 #import "MarbleCollectionVie
好的,这是非常好的 jquery slider 。 http://srobbin.com/jquery-plugins/pageslide 我所做的是 http://mbu.mn/test 问题来了。
...有人可以解释一下区别吗? 我在命令提示符下输入的内容: sys.path.append('M:/PythonMods') import qrcode myqr = qrcode.make("ra
我不时在我的服务器上运行 bash 脚本,我正在尝试编写一个脚本来监视日志文件夹并在文件夹超出定义的容量时压缩日志文件。我知道有更好的方法来做我目前正在尝试做的事情,非常欢迎您提出建议。下面的脚本抛出
我是 Groovy & Grails 的新手,我觉得事情不必那么难看……那么我怎样才能让这段代码更好看呢? 这是一个 Grails Controller 类,去掉了一些无趣的部分。尽量不要太挂断我的
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
在这个查询中,除了 5 个连接表之外,我试图使用第 6 个表“Days”将值与连接中的三个表进行比较。但它给了我错误,我不能在连接中使用子查询。 select a.ID, a.Name, a.AMT,
我不想通过 Xpath 提取,我想要更清晰的代码。谢谢我的世界兄弟 来自 Xpath,好的!! Assert.assertEquals("Digite um e-mail ou número de t
这个问题已经有答案了: Google Map is not loading due to inflate exception (3 个回答) 已关闭 9 年前。 我知道有很多关于此的帖子,但我就是无法
我的问题.. a.) 使用内存分配创建一个 float 组来存储 GPA 分数10名学生。为其分配值(您的选择) b.) 找出该数组中的最大 GPA。 c.) 将此数组的内容写入文件 alloc.tx
我最近要制作 Sequelize 。 我有 2 个表,data_track 和 car_detail。我想尝试关联那 2 个表,但它从未关联过。 总是返回错误SequelizeEagerLoading
我有一些代码在 LINQ 中根本无法工作的问题,但它确实可以作为一个简单的 for..each 工作。任何解释和解决方案将不胜感激。 我有 3 个类,Users、User 和 UserPermissi
我正在设计我的第一个大型数据库,并想检查我是否可以提供表关系。 我正在设计一个网络应用程序,其中 用户可以在团队中玩游戏 每个游戏都有其类别 用户为游戏创建他们的团队并选择他们的团队类别 每个游戏都启
我很抱歉成为一个 CSS 菜鸟,希望有人能指导我正确的方向。 我需要帮助的网页可以在 http://filefx.com 找到 当您点击该页面时,您会注意到“选择文件”图标和“上传文件”图标不在同一行
我已经尝试过这个我在网上找到的演示代码练习并创建了这个 slider ,使用滚动条更容易获得它,因为它们已经是为此制作的脚本。现在我正在尝试修改此脚本及其 css,以将滚动条更改为左右两侧的箭头。我已
最近我对 CSS 很感兴趣。学习不同的东西。 我正在尝试像这样放置三个 div: http://i.stack.imgur.com/miN9G.png 我得到的: http://i.stack.img
我是一名优秀的程序员,十分优秀!