locked
a couple of linq questions RRS feed

  • Question

  • User1034446946 posted

    Hi

    I am moving my mapping over to linqs and have two issues:

    1, can i set the previous model as a field in the current one

    public class Model1
    {
    public string name {get;set;}
    public ICollection<ModelA> modelAs {get;set;}
    }
    
    public class ModelA
    {
    public Model1 source {get;set;}
    public string name {get;set;}
    
    }

    so i have

    var model = //some list of Model1 with multiple modela's
    
    var map = model.Select(model => new Model1{
    name = model.Name,
    modelAs = model.modelAs.Select(s => new ModelA{source =?, name = s.name})
    });

    since the model prior doesn't have a reference how do i give the source a value?

    my other issue is can I add to the same list at different times?

    var map = model.Select(model => name = model.name,
                                    modelas = model.modelas.Select(mas => new modelas{ name = mas.name}),
                                    modelas.Concat(model.modelas.Select(mas => new modelas{ name = mas.name}));

    is that ok? (hopefully you can understand what i am trying to do, with the very rough code)

    Monday, November 2, 2020 3:38 PM

Answers

  • User1034446946 posted

    i don't think its possible, i have change to static methods for now will re look at it later

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2020 1:50 AM

All replies

  • User1312693872 posted

    Hi,enenDaveyBoy

    Does the code want to load another object in one object to get the value? Do you have some relations between the two models? When a

    relation is set between them,then you can use 'include' to load related objects without writing it yourself.

    You can check this link:

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/relationships#loading-related-objects

    As an example, you can get the two models' datum by using :

    var model = _db.ModelAs
          .Include(s => s.source).ToList();

    Best Regards,

    Jerry Cai

    Tuesday, November 3, 2020 5:42 AM
  • User1034446946 posted

    thanks, but this has nothing to do with Identity Framework, so include wont work, its just mapping complex pocos, but i want bi directional connection

    Tuesday, November 3, 2020 3:43 PM
  • User475983607 posted

    EnenDaveyBoy

    thanks, but this has nothing to do with Identity Framework, so include wont work, its just mapping complex pocos, but i want bi directional connection

    Identity framework?  Do you mean Entity Framework?  My best guess is you are confusing code first constructs and EF queries with designing POCOs.  Your POCO should have the following pattern.

        public class Parent
        {
            public string Name { get; set; }
            public List<Child> Children { get; set; }
        }
    
        public class Child
        {
            public string Name { get; set; }
        }

    If you have the child...

    Parent.Children[0];

    Then you already have the Parent too.  I'm a bit confused what your use case might be...

    Keep in mind, Entity Framework uses your original construct to generate a SQL script.   This allows LINQ to start form the child or the parent.  Are you trying to extend LINQ or create a custom DbContext?

    Tuesday, November 3, 2020 4:49 PM
  • User1034446946 posted

    hi, sorry yep ment it has nothing to do with entity framework.

    I want child to link back to it's parent so i can do i bi-directional processing.

    I know i can do it manually, but i would like to do it in with Linqs

    eg

    adding a Parent property to child and doing

    var parent = new Parent{Name = "boo", Children = new List<Child>()};
    var child = new Child{Parent = parent, Name = "ahhh"};
    parent.Children.Add(child);

    but I want to do it with linqs

    var needsMapping = new Parent{Name = "Boo", Children = new List<Child>{new Child{Name="ahhh"}}};

    var new map = new Parent{Name = needsMapping.Name, Child = needsMapping.Children.Select(s => new Child{Parent = ?? , Name = s.Name})};

    since the parent has no reference I can't reference it,

    Wednesday, November 4, 2020 12:23 AM
  • User475983607 posted

    I want child to link back to it's parent so i can do i bi-directional processing.

    What is "bi-directional processing" in your design?  Again, it seems your question is related to how LINQ works.  You want some kind of LINQ to ??? framework.  ie. LINQ to Entities, LINQ to SQL, LINQ to XML...

    Wednesday, November 4, 2020 12:54 AM
  • User1034446946 posted

    i don't think its possible, i have change to static methods for now will re look at it later

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2020 1:50 AM