How to return same object with linq as with lambda

c# entity-framework entity-framework-core linq

Question

I'm trying to use object with Entity Framework Core when I use linq query. But object is always anonymous, which I don't want. I have this code:

                var query = (from c in context.UserTest
                         join k in context.OneToTest on c.FkOneToTestId equals k.Id
                         select new { c, k }).ToList();

which is anonymous object. But with this code:

 var test = context.UserTest
            .Include(one => one.OneToTest)
            .ToList();

I want to have same object in query and test.Now in test is List<UserTest> and in query List<'a>. Is there any easy way how to do it with linq?

1
1
11/10/2018 5:42:43 PM

Accepted Answer

The equivalent of

var test = context.UserTest.Include(one => one.OneToTest).ToList();

is:

var test = (from c in context.UserTest.Include(one => one.OneToTest)
            select c).ToList();

The only way I know to not use Include is to create a new object, where you don't need the join:

var test = (from c in context.UserTest
            select new UserTest
                {
                    Id = c.Id,
                    OneToTest = c.OneToTest,
                    // ...
                }).ToList();
0
11/11/2018 12:48:38 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow