How to return same object with linq as with lambda











up vote
1
down vote

favorite
1












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?










share|improve this question


















  • 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















up vote
1
down vote

favorite
1












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?










share|improve this question


















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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?










share|improve this 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?







c# entity-framework linq entity-framework-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























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





    share|improve this answer

























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





      share|improve this answer























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





        share|improve this 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();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 0:48









        Ruard van Elburg

        4,77321125




        4,77321125






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine