使用QueryMultiple的Dapper多重映射 [英] Dapper Multi Mapping with QueryMultiple

查看:232
本文介绍了使用QueryMultiple的Dapper多重映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该过程返回多个结果集。我正在用dapper执行此操作。

I have a stored procedure that returns multiple result sets. I'm executing this with dapper.

结果集之一是Person JOIN Checks,其中Person可以有很多支票。

One of the result sets is Person JOIN Checks, where Person can have many Checks.

最终目标是要有不同的人员对象,这些对象具有检查对象的集合。

The end goal is to have distinct person objects that have a collection of check objects.

QueryMultiple 给了我 Sqlmapper.GridReader 。我看到 SqlMapper.GridReader.Read()的重载是 Func< TFirst,TSecond,TReturn>

QueryMultiple gives me a Sqlmapper.GridReader. I see an overload of SqlMapper.GridReader.Read() that takes a Func<TFirst, TSecond, TReturn>.

是否有使用此示例的示例?

Is there an example of how to use this?

推荐答案

这是我的工作方式:

var q = _sqlConnection.QueryMultiple("MySproc",
                                     myParams,
                                     commandType: CommandType.StoredProcedure);
var set1 = q.Read<Set1Type>();

var set2Func = new Func<Person, Check, Person>((p, c) => {
    p.CheckAlert = c;
    return p;
});

var set2 = q.Read(set2Func, "CheckId")
            .GroupBy(x => x.PersonId)
            .Select(x => {
                var person = x.First();
                person.Checks = x.Select(p => p.Check).ToArray();
                person.Check = null; // i really don't like this
                return person;
            })
            .ToArray();

正如评论所说,我不喜欢Person对象上不需要的check属性。

As the comment says, I don't like the unneeded check property on the Person object.

我还是很想听听这样做的更好方法。

I'd still love to hear of a better way of doing this.

这篇关于使用QueryMultiple的Dapper多重映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆