How to return same object with linq as with lambda
up vote
1
down vote
favorite
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?
c# entity-framework linq entity-framework-core
add a comment |
up vote
1
down vote
favorite
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?
c# entity-framework linq entity-framework-core
1
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
Does my answer help you?
– Ruard van Elburg
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
c# entity-framework linq entity-framework-core
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?
c# entity-framework linq entity-framework-core
c# entity-framework linq entity-framework-core
asked Nov 10 at 17:42
marcel novák
134
134
1
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
Does my answer help you?
– Ruard van Elburg
yesterday
add a comment |
1
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
Does my answer help you?
– Ruard van Elburg
yesterday
1
1
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
Does my answer help you?
– Ruard van Elburg
yesterday
Does my answer help you?
– Ruard van Elburg
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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();
add a comment |
up vote
0
down vote
accepted
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();
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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();
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();
answered Nov 11 at 0:48
Ruard van Elburg
4,77321125
4,77321125
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241707%2fhow-to-return-same-object-with-linq-as-with-lambda%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's unclear what are you asking. What you call test is the EF Core recommended (intended) way to do what you call query. In LINQ to Entities we don't use manual joins as in the regular LINQ, because we don't need them - navigation properties do that for us.
– Ivan Stoev
Nov 10 at 17:49
Does my answer help you?
– Ruard van Elburg
yesterday