使用linq对ado.net查询出来dataset集合转换成对象(查询出来的数据结构为一对多)
public async Task<IEnumerable<QuestionAllInfo>> GetAllQuestionByTypeIdAsync(int id)
{
string sql = "SELECT q.Id as questionId,i.Id as instanceId,q.name as questionName,q.Intention as questionIntention,i.`Name` as instancesName FROM `t_questions` q LEFT JOIN t_instances i on q.Id = i.QuestionId WHERE q.QuestionTypeId = @p1";
MySqlParameter[] mySqlParameters = new MySqlParameter[] { new MySqlParameter { MySqlDbType = MySqlDbType.Int32, Value = id, ParameterName = "@p1" } };
var dataSet = await SQLHelp.ExecuteQueryAsync(sql, mySqlParameters);
var table = dataSet.Tables[];
List<Temp> temps = new List<Temp>();
if (table.Rows.Count <= )
{
return null;
}
foreach (DataRow row in table.Rows)
{
temps.Add(new Temp
{
QuestionId = (int)row["questionId"],
QuestionIntention = row["questionIntention"].ToString(),
QuestionName = row["questionName"].ToString(),
InstanceId = IsIntDBNULL(row["instanceId"]),
InstanceName = row["instancesName"].ToString()
}); ;
}
var result = temps.GroupBy(t => new { t.QuestionId, t.QuestionIntention, t.QuestionName })
.Select(question =>
new QuestionAllInfo
{
QuestionId = question.Key.QuestionId,
QuestionName = question.Key.QuestionName,
QuestionIntention = question.Key.QuestionIntention,
Instances = question.Select(q => new Instance
{
Id = q.InstanceId,
Name = q.InstanceName,
QuestionId = q.QuestionId
})
});
return result;
}
要转换成的实体对象
class Temp
{
public int QuestionId { get; set; }
public string QuestionIntention { get; set; }
public string QuestionName { get; set; }
public int? InstanceId { get; set; }
public string InstanceName { get; set; }
} public class QuestionAllInfo
{
public int QuestionId { get; set; }
public string QuestionIntention { get; set; }
public string QuestionName { get; set; }
public IEnumerable<Instance> Instances { get; set; }
} public class Instance
{
public int Id { get; set; }
public string Name { get; set; }
public int QuestionId { get; set; }
}
使用linq对ado.net查询出来dataset集合转换成对象(查询出来的数据结构为一对多)的更多相关文章
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串
在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的 ...
- 使用HIBERNATE的SQL查询并将结果集自动转换成POJO
在某些场合下,我们可能想使用HIBERNATE的框架提供的SQL查询接口,但是,由于实体没有做映射,HIBERNATE不能把结果集转换成你想要的List<POJO>,本文讨论如何在这种情况 ...
- c#实现list,dataset,DataTable转换成josn等各种转换方法总和
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Refle ...
- DataSet 反射转换成 List<T>
/// <summary> /// DataSet转换成指定返回类型的实体集合 /// </summary> /// <typeparam name="T&qu ...
- mysql 查询 int类型日期转换成datetime类型
数据库日期类型是int类型的,该查询结果是datetime类型的 SELECT from_unixtime( `时间列名` ) FROM 表名 如果原来类型是datetime类型,查询结果要是int类 ...
- FMDB使用的一点心得:数据库创建、制表、查询等以及image转换成二进制nsdata保存到数据库中
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012951123/article/details/36871955 <span style= ...
- 十四、C# 支持标准查询运算符的集合接口
支持标准查询运算符的集合接口. System.Linq.Enumeralbe类提供的一些常用的API 来执行集合处理 1.匿名类型 2.隐匿类型的局部变量 3.集合初始化器 4.集合 5.标准查询运算 ...
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
随机推荐
- tornado 之 RequestHandler(请求)
RequestHandler from tornado.web import ReuqestHandler 一.利用HTTP协议想服务器传递参数 提取url的特定部分 http://127.0.0.1 ...
- JPA-save()方法会将字段更新为null的解决方法
今天在开发上碰到一个问题,在做页面展示的时候传给前端十个字段,前端修改了其中3个的值,所以只传了3个值给后端,其余字段默认为null,更新后其他7个字段在全部变为了空值. 在前端没法全量回传所有属性的 ...
- 201871010102-常龙龙《面向对象程序设计(java)》第八周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- shell 之while两种写法
1.while[] #!/bin/dash min= max= while [ $min -le $max ] do echo $min min=`` done 2. while(()) #!/bin ...
- NOIP 2016 回文日期
洛谷P2010 https://www.luogu.org/problemnew/show/P2010 JDOJ 3313 https://neooj.com:8082/oldoj/problem.p ...
- Python进阶-VI 生成器函数进阶、生成器表达式、推导式
一.生成器函数进阶 需求:求取移动平均数 1.应用场景之一,在奥运会气枪射击比赛中,每打完一发都会显示平均环数! def show_avg(): print('你已进入显示移动平均环数系统!') a ...
- Codeforces Round #605 (Div. 3) 题解
Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...
- 使用arthas 生成火焰图分析jvm
arthas 是阿里巴巴开源的强大的jvm 应该分析工具,以下是使用arthas 生成jvm 火焰图的一个学习 项目使用docker-compose 运行,对于生成的火焰图使用nginx 提供一个访问 ...
- css3中@font-face模块自定义字体
一.@font-face模块介绍 @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,在Web的开发中使用字体不再只能使用W ...
- [LeetCode] 374. Guess Number Higher or Lower 猜数字大小
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...