2

I have a Linq query which is selecting 2 columns(that can be any 2 from all columns) dynamically based on some condition.I need to map the query result in to below model irrespective of selected column names

public class FinalModel
{ 
    public string Text { get; set; }
    public string Id { get; set; }
}

Currently I am using reflection to map the result in to that model because i am getting some anonymous list of objects and it is working fine, But I want to remove that reflection and need to add the mapping in the select itself, my current implementation is like below

 string column1 = "Name" //can be other columns also
 string column2 = "Age"
 var result = _context.table1
                      .Select("new ("+ column1 +","+ column2 +")")
                      .Distinct()
                      .Take(10) // having more records in table
                      .ToDynamicList()
                      .Select(x => new FinalModel()
                       {
                           Id = x.GetType().GetProperty(column1).GetValue(x).ToString(),
                           Text = x.GetType().GetProperty(column2).GetValue(x).ToString(),
                       });
                      

The above code is working fine but I need to remove the below section

       .Select(x => new FinalModel()
              {
                 Id = x.GetType().GetProperty(column1).GetValue(x).ToString(),
                 Text = x.GetType().GetProperty(column2).GetValue(x).ToString(),
              });

Is there any way to remove the refletion and add that model mapping directly inside Select("new (column1,column2)")

  1. Is there any way to add orderBy with Column2 variable?

2 Answers 2

2

You can use generic versions of Select and ToDynamicList and OrderBy($"{column2}") for sorting:

var result = _context.table1
    .Select<FinalModel>($"new ({column1} as Id, {column2} as Text)")
    .Distinct()
    .OrderBy("Text")
    .Take(10)
    .ToDynamicList<FinalModel>();

Or if you want to stick with dynamic:

var result = _context.table1
    .Select($"new ({column1}, {column2})")
    .Distinct()
    .OrderBy($"{column2}")
    .Take(10) 
    .ToDynamicList()
    .Select(d => new FinalModel()
    {
        Id = d[column1].ToString(),
        Text = d[column2].ToString(),
    })
    .ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, it allows me to remove the reflection but the .OrderBy($"{column2}") is not working
@user3501613 can you please elaborate on "not working"? It results in exception? Or it doesn't select top results sorted?
ya it is not coming is sorted order, there is no exception
@user3501613 it seems Distinct breaks ordering, Try moving it after it (see the updated code).
2
  1. You need to use .Select<T> instead of just .Select() to make sure that the selected entity the correct type. So in your case you need .Select<FinalModel>.

  2. Use the as cast operator to "rename" the properties from the source-entity to the destination entity (FinalModel)

  3. If you want the result to be typed, also use .ToDynamicList<FinalModel>().

Full example code below:

using System.Linq.Dynamic.Core;

class Program
{
    static void Main(string[] args)
    {
        var myQuery = new[] { new XModel { Age = "1", Name = "n" } }.AsQueryable();

        string column1 = "Name";
        string column2 = "Age";

        var result = myQuery
            .Select<FinalModel>("new (" + column1 + " as Id, " + column2 + " as Text)")
            .Distinct()
            .Take(10)
            .ToDynamicList<FinalModel>();

        Console.WriteLine(result[0].Id + "  " + result[0].Text);
    }


    public class XModel
    {
        public string Name { get; set; }
        public string Age { get; set; }
    }

    public class FinalModel
    {
        public string Text { get; set; }
        public string Id { get; set; }
    }
}

2 Comments

this is working fine, can you please suggest how to orderby with the same query
OrderBy should just work as proposed by @guru-stron, no magic is needed there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.