[英]Anonymous type to List of object
我正在使用 dapper 从数据库中获取数据,如下所示(SQL Server)
Column1 column2 column3
Type1 (1,sub1,sub1 descreption),(2,sub2,sub2 description) test
Type2 (3,sub3,sub3 descreption),(4,sub4,sub4 description) test
类如下所示:
public class Types
{
public string Column1 { get; set; }
public IEnumerable<Subs> Column2 { get; set; }
public string Column3 { get; set; }
}
public class Subs
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
}
我正在尝试将 Column2 转换为 IEnumerable 并且无法这样做。 有人可以建议一种方法吗? 我是否需要更改从数据库中选择数据的方式?
实际上,第 2 列本身应该是一个与您当前表有很多关系的表,而您目前正在做的是一个非常糟糕的做法,并且可能有很多缺点。
正确的方法:创建一个以 Types 表 ID 作为外键的表:
CREATE TABLE Subs(
id int,
typeId int foreign key references [Types](id), // you must also add an id could to Types table
name nvarchar(128), //any size that you need it
description nvarchar(max)
)
和班级:
public class Sub
{
public int id { get; set; }
public int typeId {get; set; }
public string name { get; set; }
public string description { get; set; }
}
关于如何使用 dapper 获取一对多关系,请看这个问题: How to query an object with one-to-many relationship using Dapper?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.