小巧玲珑取任何pre定义的类型 [英] Dapper Fetch any pre-defined type

查看:329
本文介绍了小巧玲珑取任何pre定义的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用小巧玲珑的获取数据,我有一个程序,它可以根据提供的参数返回一出多个predefined类型。现在,在运行时我需要知道哪些的IEnumerable< T> 它包含,尝试使用列表,然后类型转换,但是这不是工作。为了提供一个例子:

Using Dapper to fetch the data, I have a procedure, which may return one out of multiple predefined types, based on the parameter supplied. Now at run time I need to know which IEnumerable<T> does it contain, tried using List and then typecasting but that's not working. To provide an example:

我有以下pre定义的类型,它可以从一个SP返回:

I have following pre-defined types, which can be returned from a SP:

AllSalesTerritory MySalesPerson PointOfSales

我已经尝试到现在是:

var dynamicResult = _connection.Query<dynamic>(query).ToList();

现在,当我们试图动态列表类型强制转换为任何上面提到的pre定义类型列表中,没有什么工作,因为它似乎在内部动态列表包含包含 DapperRow ,我不知道如何使用 DapperRow 来实现这一目标。我已经尝试了所有可用的C#操作符

Now when we try to typecast the dynamic list to any of the pre-defined type list mentioned above, nothing is working, as it seems internally Dynamic List contains contains a DapperRow, I am not sure how to utilize DapperRow to achieve the objective. I have tried all available C# operators is, as.

任何指针,如何实现它,因为这是一个企业的要求,我不能变通解决

Any pointer, how to achieve it, since this is a business requirement, which I cannot work around

我试图用一种变通方法 SqlMapper.GridReader ,如下所示,但是它的用处不大在这种情况下,将工作,当我有多个结果集一个给定的顺序,我不知道,一个给定的结果集是否可以作为查询结果的一部分:

I tried a workaround using SqlMapper.GridReader, as follows, but its of little use in this case, as that would work, when I there are multiple result sets in a given order and I am not sure, whether a given Result set is available as part of query result:

var multipleResultReader = _connection.QueryMultiple(query);

var result1 = !multipleResultReader.IsConsumed
                ? multipleResultReader.Read<AllSalesTerritory>() : null;

var result2 = !multipleResultReader.IsConsumed
                ? multipleResultReader.Read<MySalesPerson>() : null;

请让我知道如果你需要更多的信息。

Please let me know if your need any more information

推荐答案

我能够实现在一个令人费解的方式,方法如下:

I am able to achieve it in a convoluted way, as follows:

的IEnumerable&LT;动态&GT; 如下:

var dynamicResult = _connection.Query<dynamic>(query).ToList();

最低1行应休息的逻辑来执行存在,不知道如何解决办法时,有没有行,只是架构

Minimum 1 row shall exist for rest of the logic to execute, Not sure how to workaround when there's no row, just schema

取第一行,并创建一个的IDictionary&LT;字符串,对象&gt;

Fetch first Row and create a IDictionary<string,object>

var firstRow = (IDictionary<string, object>) dynamicResult[0];

在一个单独的集合取钥匙

Fetch keys in a separate collection

var columnNameList = firstRow.Keys.ToList();

现在对每个期望类型经过每个简单属性,忽略了收集和复杂类型和匹配与在字典中,其中包括自定义映射属性原始模式被应用,一旦我们推断出有效的类型,通过填集合类似的IDictionary ,其中为每列键有相应的价值,从而相应的列表,它不是空的结果。

Now for each expected type go through each simple property, ignoring the collection and complex type and match with the original schema in the dictionary, which includes the custom mapping attribute is applied, once we deduce the valid type, fill the collection by similar IDictionary where for each column key there's a corresponding value and thus a corresponding list which is not null is the result.

不知道是否有更简单的方法,它是开箱即用,也可以处理是有由该查询只是一个架构

Not sure if there's a simpler way, which is out of the box and can also handle is there's no data / row returned by the query just a schema

这篇关于小巧玲珑取任何pre定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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