LINQ - Add property to results

前端 未结 5 1087
日久生厌
日久生厌 2020-12-18 23:15

Is there a way to add a property to the objects of a Linq query result other than the following?

var query = from x in db.Courses
                select new
         


        
相关标签:
5条回答
  • 2020-12-18 23:43

    Add it with partial classes:

    public partial class Courses
    {
        public String NewProperty { get; set; }
    }
    

    Then you can assign it after you've created the object.

    0 讨论(0)
  • 2020-12-18 23:43
    • ServiceStack has a built-in way to handle this with the PopulateWith method.

      • Here's a code example.

        foreach (var item in results)
        {
            var test1 = new ItemDto().PopulateWith(item);
            test1.extraField1 = "extra";
            response.Add(test1);
        }`
        
    • And if you're not using ServiceStack, you can always use AutoMapper.

      CreateMap<Foo, Bar>().ForMember(x => x.ExtraBarProperty, opt => opt.Ignore());

    0 讨论(0)
  • 2020-12-18 23:44

    eking's answer will be the most straightforward approach.

    If that doesn't work for you (because you need to pass the results around or whatever), and assuming the class you're dealing with already defines the property you want to set, you could create a copy constructor or factory method that takes an existing instance plus the value of the property you want to set:

    var query = from x in db.Courses
                select new Course(x, valueOfNewProperty);
    

    Alternatively, if Course doesn't define the property, you could subclass it and use the same approach:

    var query = from x in db.Courses
                select new CourseWithExtraProperty(x, valueOfNewProperty);
    

    (obviously, pick a better name for your subclass)

    Again, though, unless you really need to do this, stick with eking's solution.

    0 讨论(0)
  • 2020-12-18 23:52

    If you are looking to dynamically add a property to an object this could be a solution.

    This is what has worked for me, I also had a concern and it was what happened with those domain objects that had many properties, the maintainability for any changes in the object was absurd, I managed to build an implementation with LINQ - ExpandObject - Reflection, which helped to keep my object dynamic and only add the additional properties that my view logic required.

    var expandedModel = db.Courses.Select(x =>
                        {
                            dynamic expandObject = new ExpandoObject();
                            expandObject.NewProperty= $"PropertyValue";
                            foreach (var property in x.GetType().GetProperties())
                            {
                                ((IDictionary<string, object>)expandObject).Add(property.Name, property.GetValue(x));
                            }
                            return expandObject;
                        }).ToList();
    
    0 讨论(0)
  • 2020-12-18 23:57

    I suppose you could return a new object composed of the new property and the selected object, like:

    var query = from x in db.Courses
                    select new
                    {
                        Course x,
                        NewProperty = true
                    };
    
    0 讨论(0)
提交回复
热议问题