What is the best way to read entity properties in "Include" with similar names? RRS feed

  • Question

  • User-140672246 posted
    Hey guys.
    I have the following query:
                        var recs = (from p in _db.Fichas
                            .Include(c => c.Tipo01)
                            .Include(c => c.Tipo02)
                            .Include(c => c.Tipo03)
                            .Include(c => c.Pessoa01)
                            .Include(c => c.Pessoa02)
                            .Include(c => c.Pessoa03)
                            where p.Id == 100
                            select p).FirstOrDefault();
    Based on the above query, I am getting and reading (I believe the wrong way) the values of the following properties:
                    var nome1 = recs.Pessoa01 != null ? recs.Pessoa01.Nome : null;
                var nome2 = recs.Pessoa02 != null ? recs.Pessoa02.Nome : null;
                var nome3 = recs.Pessoa03 != null ? recs.Pessoa03.Nome : null;
                var tip1 = recs.Tipo01 != null ? recs.Tipo01.ModeloA : null;
                var tip2 = recs.Tipo02 != null ? recs.Tipo02.ModeloA : null;
    Could someone please help me and show me a more decent way of doing this task?
    Sunday, May 23, 2021 11:59 AM

All replies

  • User475983607 posted

    When asking for help on the forum the community needs basic information.  The entity design, sample data, expected results, and the actual results.  Otherwise; it's hard to provide an accurate soliton.  

    IMHO, the sample code points to an overall database normalization design issue.  

    Sunday, May 23, 2021 2:04 PM
  • User-140672246 posted
    Thanks for the feedback, mgebhard. I hope that by reading this message, you will be well.
    Yes, there is a normalization problem. Unfortunately I have to deal with this for now. I am maintaining software developed by another professional.
    If possible, I would like you or other colleagues in the group to help me resolve this issue that I posted ??
    Sunday, May 23, 2021 3:42 PM
  • User-474980206 posted

    use more modern c# syntax:

    var nome1 = recs.Pessoa01?.Nome;
    Sunday, May 23, 2021 5:32 PM
  • User-140672246 posted
    Hello, Bruce.
    I wanted something like looping and using GetType () / GetProperty. But I don't know how to apply these methods. As the names of the entities are similar, I thought there would be something more efficient. Any idea?
    Sunday, May 23, 2021 8:36 PM
  • User287926715 posted

    Hi Link_,

    You can refer to the following method of use.

    using System;
    using System.Reflection;
    class MyClass
        private int myProperty;
        // Declare MyProperty.
        public int MyProperty
                return myProperty;
    public class MyTypeClass
        public static void Main(string[] args)
                // Get the Type object corresponding to MyClass.
                Type myType=typeof(MyClass);
                // Get the PropertyInfo object by passing the property name.
                PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
                // Display the property name.
                Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
            catch(NullReferenceException e)
                Console.WriteLine("The property does not exist in MyClass." + e.Message);

    Best Regards,


    Wednesday, June 2, 2021 3:03 AM