C#,entity framework,linq如何排除属性
C#, entity framework, linq how to exclude property
public class MyObj
{
[Key]
public int id {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public int test3 {get; set;}
public int test4 {get; set;}
public int test5 {get; set;}
public int? test6 {get; set;}
public DateTime? test7 {get; set;}
public string test8 {get; set;}
public string test9 {get; set;}
public string test10 {get; set;}
public string test11 {get; set;}
public string test12 {get; set;}
public DateTime? test13 {get; set;}
public DateTime? test14 {get; set;}
public DateTime? test15 {get; set;}
public string test16 {get; set;}
public string tes17 {get; set;}
public int test18 {get; set;}
public DateTime? test19 {get; set;}
public bool test20 {get; set;}
[ForeignKey("test3")]
public virtual Child1 child1 { get; set; }
[ForeignKey("test4")]
public virtual Child2 child2 { get; set; }
[ForeignKey("test5")]
public virtual Child3 child3 { get; set; }
[ForeignKey("test18")]
public virtual Child4 child4 { get; set; }
public virtual ICollection<Child1> child5 { get; set; }
}
var myobj = unitwork.myobjRepository.get();
我只想把id和test 1拉到test20,没有child1,child2,child3,child4和child5。
现在,我愿意,
myobj.Select(x => new {
id = x.id,
test1 = x.test1
...
...
test20 = x.test20
}));
但我不认为这是正确的方法..请告诉我,
如果 属性 名称相同,则 稍微 更简单:
myobj.Select(x => new {
x.id,
x.test1
...
...
x.test20
}));
如果您只是投影没有转换的属性,匿名类型将使用相同的 属性 名称。
如果您担心创建匿名类型,那么您可以创建一个仅具有您需要的属性并投射到的命名类型,但是使用您所展示的匿名类型没有任何问题。
您想这样做的原因是什么?如果您考虑不要使 sql 服务器过载,则应检查延迟加载。在您访问这些属性之前,不会加载 child1、child2、child3、child4、child5,并且您将拥有可用的 MyObj class(对象)。
如果你像以前那样做,你最终会得到匿名对象。在某些情况下可能没问题,但在其他情况下则不然。
也可能是一个解决方案是创建另一个没有不必要字段的 class 并将 select 变成新 class.
的实例
myobj.Select(x => new NewClass() {
id = x.id,
test1 = x.test1
...
...
test20 = x.test20
}));
public class MyObj
{
[Key]
public int id {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public int test3 {get; set;}
public int test4 {get; set;}
public int test5 {get; set;}
public int? test6 {get; set;}
public DateTime? test7 {get; set;}
public string test8 {get; set;}
public string test9 {get; set;}
public string test10 {get; set;}
public string test11 {get; set;}
public string test12 {get; set;}
public DateTime? test13 {get; set;}
public DateTime? test14 {get; set;}
public DateTime? test15 {get; set;}
public string test16 {get; set;}
public string tes17 {get; set;}
public int test18 {get; set;}
public DateTime? test19 {get; set;}
public bool test20 {get; set;}
[ForeignKey("test3")]
public virtual Child1 child1 { get; set; }
[ForeignKey("test4")]
public virtual Child2 child2 { get; set; }
[ForeignKey("test5")]
public virtual Child3 child3 { get; set; }
[ForeignKey("test18")]
public virtual Child4 child4 { get; set; }
public virtual ICollection<Child1> child5 { get; set; }
}
var myobj = unitwork.myobjRepository.get();
我只想把id和test 1拉到test20,没有child1,child2,child3,child4和child5。
现在,我愿意,
myobj.Select(x => new {
id = x.id,
test1 = x.test1
...
...
test20 = x.test20
}));
但我不认为这是正确的方法..请告诉我,
如果 属性 名称相同,则 稍微 更简单:
myobj.Select(x => new {
x.id,
x.test1
...
...
x.test20
}));
如果您只是投影没有转换的属性,匿名类型将使用相同的 属性 名称。
如果您担心创建匿名类型,那么您可以创建一个仅具有您需要的属性并投射到的命名类型,但是使用您所展示的匿名类型没有任何问题。
您想这样做的原因是什么?如果您考虑不要使 sql 服务器过载,则应检查延迟加载。在您访问这些属性之前,不会加载 child1、child2、child3、child4、child5,并且您将拥有可用的 MyObj class(对象)。 如果你像以前那样做,你最终会得到匿名对象。在某些情况下可能没问题,但在其他情况下则不然。 也可能是一个解决方案是创建另一个没有不必要字段的 class 并将 select 变成新 class.
的实例myobj.Select(x => new NewClass() {
id = x.id,
test1 = x.test1
...
...
test20 = x.test20
}));