C# 使用实体框架核心拆分表。展平桌子以掌握细节
我想把这个扁平表放到类级别的主细节层次结构中C# 使用实体框架核心拆分表。展平桌子以掌握细节,c#,asp.net,.net,database,entity-framework,C#,Asp.net,.net,Database,Entity Framework,我想把这个扁平表放到类级别的主细节层次结构中 | Type | DateTime | Value | ----------------------------- | 4711 | 2018-01-01 | 0.7 | | 4711 | 2018-01-02 | 0.8 | | 4711 | 2018-01-03 | 0.9 | | 4711 | 2018-01-04 | 1.0 | | 4712 | 2018-01-01 | 3.2 | | 4712 | 2018-01-
| Type | DateTime | Value |
-----------------------------
| 4711 | 2018-01-01 | 0.7 |
| 4711 | 2018-01-02 | 0.8 |
| 4711 | 2018-01-03 | 0.9 |
| 4711 | 2018-01-04 | 1.0 |
| 4712 | 2018-01-01 | 3.2 |
| 4712 | 2018-01-02 | 2.7 |
| 4712 | 2018-01-03 | 5.6 |
| 4712 | 2018-01-04 | 1.9 |
阶级结构
使现代化
我想说清楚。我需要一个解决方案,可以将IQueryable转发到以下服务。解决方案不是手动分组数据,而是使用列表进行处理
所以我需要在模型级别上解决这个问题。这段代码有效吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication86
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Type", typeof(int));
dt.Columns.Add("DateTime", typeof(DateTime));
dt.Columns.Add("Value", typeof(float));
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-01"), 0.7 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-02"), 0.8 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-03"), 0.9 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-04"), 1.0 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-01"), 3.2 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-02"), 2.7 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-03"), 5.6 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-04"), 1.9 });
var groups = dt.AsEnumerable().GroupBy(x => x.Field<int>("Type")).ToList();
List<TypeMaster> data = (from g in groups
select new { grp = new TypeMaster() { Type = g.Key }, rows = g })
.Select(x => new TypeMaster() {
Type = x.grp.Type,
Values = x.grp.Values = x.rows.Select(y => new TypeValue() {
TimeStamp = y.Field<DateTime>("DateTime"),
Value = y.Field<float>("Value"),
Index = x.grp
}).ToList()
}).ToList();
}
}
[Table("TypeValues")]
public class TypeMaster
{
[Column("Type")]
public int Type { get; set; }
[ForeignKey("Type")]
public virtual ICollection<TypeValue> Values { get; set; }
}
[Table("TypeValues")]
public class TypeValue
{
[Column("DateTime")]
public DateTime TimeStamp { get; set; }
[Column("Value")]
public float? Value { get; set; }
public TypeMaster Index { get; set; }
}
}
只需使用GroupByx=>x.Type表拆分不能用于规范化不规范的数据模型。很抱歉,我忘了提到这是不可能的。我需要通过设计类和使用ModelBuilder@GertArnold:好的,我的其他选择是什么?@masterchris_99表格拆分用于一对一关系。您唯一的选择是使用查询GroupBy。您无法在模型级别解决它。这与实体框架无关。这是一个简单的分组方式。我要把保险箱退了。在您的例子中,执行SQL语句,将其转换为列表,然后返回。因此,所有进一步的筛选都将对已加载到内存中的数据执行。您只需将我的代码转换为ObjectQuery即可。请参阅:@jdweng为什么回答一个问题时会问另一个问题,后面跟着无法解释的代码?未来的读者不会花力气去挖掘代码并找到回答问题的相关部分。这就是为什么尝试这个答案,事实上,这个答案不是很有用。最好突出答案的本质。我认为其本质是:将分组设计成一个大师班。正如Ivan在上面所说,这很可能是一个可以进一步扩展的IQueryable。我认为DataTable部分只是让人困惑。在没有输入的情况下,如何测试代码?我的解决方案是回答关于将数据表展平到类的OP问题。我没有主细节应用程序,无法使用OPs环境进行测试,这就是为什么我说尝试。我不知道OP是否需要改变他的工作环境。
{
"Result": [
{
"Type": "4711",
"Values": [
{
"TimeStamp": "2018-01-01",
"Value": "0.7"
},
{
"TimeStamp": "2018-01-02",
"Value": "0.8"
},
{
"TimeStamp": "2018-01-03",
"Value": "0.9"
},
{
"TimeStamp": "2018-01-04",
"Value": "1.0"
}
]
},
{
"Type": "4712",
"Values": [
{
"TimeStamp": "2018-01-01",
"Value": "3.2"
},
{
"TimeStamp": "2018-01-02",
"Value": "2.7"
},
{
"TimeStamp": "2018-01-03",
"Value": "5.6"
},
{
"TimeStamp": "2018-01-04",
"Value": "1.9"
}
]
}
]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication86
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Type", typeof(int));
dt.Columns.Add("DateTime", typeof(DateTime));
dt.Columns.Add("Value", typeof(float));
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-01"), 0.7 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-02"), 0.8 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-03"), 0.9 });
dt.Rows.Add(new object[] {4711, DateTime.Parse("2018-01-04"), 1.0 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-01"), 3.2 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-02"), 2.7 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-03"), 5.6 });
dt.Rows.Add(new object[] {4712, DateTime.Parse("2018-01-04"), 1.9 });
var groups = dt.AsEnumerable().GroupBy(x => x.Field<int>("Type")).ToList();
List<TypeMaster> data = (from g in groups
select new { grp = new TypeMaster() { Type = g.Key }, rows = g })
.Select(x => new TypeMaster() {
Type = x.grp.Type,
Values = x.grp.Values = x.rows.Select(y => new TypeValue() {
TimeStamp = y.Field<DateTime>("DateTime"),
Value = y.Field<float>("Value"),
Index = x.grp
}).ToList()
}).ToList();
}
}
[Table("TypeValues")]
public class TypeMaster
{
[Column("Type")]
public int Type { get; set; }
[ForeignKey("Type")]
public virtual ICollection<TypeValue> Values { get; set; }
}
[Table("TypeValues")]
public class TypeValue
{
[Column("DateTime")]
public DateTime TimeStamp { get; set; }
[Column("Value")]
public float? Value { get; set; }
public TypeMaster Index { get; set; }
}
}