jeudi 2 juillet 2015

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();

I just want to pull id and test 1 to test20, without child1, child2, child3, child4 and child5.

For now, I do,

myobj.Select(x => new {
    id = x.id,
    test1 = x.test1
    ...
    ...
    test20 = x.test20
})); 

but I don't think this is right way.. Please advise me,

Aucun commentaire:

Enregistrer un commentaire