1

I have an IQueryable<MainObjectTest> in which there are several fields, but I only need to pull out certain ones (Title and NestedObject).

How can I do this with dynamic select expression?

   public class TestIQueryable
    {

        public IQueryable GetObjectData(IQueryable<MainObjectTest> data)
        {
            IQueryable requredDataFields = data.Select("new(Title, NestedObject)");
            return requredDataFields;
        }
        public class MainObjectTest
        {
            public string Title { get; set; }
            public DateTime Date { get; set; }
            public NestedClassTest NestedObject { get; set; }
        }

        public class NestedClassTest
        {
            public string Field1 { get; set; }
            public string Field2 { get; set; }
            public string Field3 { get; set; }
        }
    }
MFerguson
  • 1,739
  • 9
  • 17
  • 30
Librain
  • 85
  • 1
  • 8
  • Usually you would use the [`Select()`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.8) extension method from LINQ. The Stack Overflow question "[LINQ to SQL - How to select specific columns and return strongly typed list](https://stackoverflow.com/questions/1094931/linq-to-sql-how-to-select-specific-columns-and-return-strongly-typed-list)" might be useful to reference here. What have you tried so far? – Jeremy Caney Mar 26 '20 at 06:40
  • I tried to pull out simple properties(Title) and it works fine for me ... Only problem to pull out class object . – Librain Mar 26 '20 at 07:00
  • Did you try with using `System.Linq.Dynamic` as `data.Select("new(Title, NestedObject.Field1)")`; – Selim Yildiz Mar 26 '20 at 08:01
  • Selim, how i can do this if there will be array of "NestedObject" ? – Librain Mar 26 '20 at 08:08
  • Yes it works too, I have added answer with array object as well. Please check – Selim Yildiz Mar 26 '20 at 08:18

3 Answers3

1

What about making an interface:

interface Interface1
{
       public string Title { get; set; }
       public NestedClassTest NestedObject { get; set; }
}

Implement the interface in you class and let GetObjectData return the interface

public IQueryable GetObjectData(IQueryable<Interface1> data)
{
    IQueryable requredDataFields = data.Select();
    return requredDataFields;
}
1

I solved my problem using model, here steps:

  1. Map all data from database into model if you have nested object => use ToList().
  2. Use DynamicLinq library to select certain fields from model as IQueryable.

Hope it helps someone!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Librain
  • 85
  • 1
  • 8
0

Use an anonymous object like this.

IQueryable requredDataFields = data.Select(x => new {  x.Title,  x.NestedObject });
Hayden
  • 2,902
  • 2
  • 15
  • 29
  • I know how to use anonymous object, but i need to use string expression to get only certain fields that coming with FRONTEND request. – Librain Mar 26 '20 at 06:30
  • @Librain Sorry for misunderstanding the question. Does this question help with what you're looking for? https://stackoverflow.com/questions/16516971/linq-dynamic-select – Hayden Mar 26 '20 at 06:34