SmartSelector is open source library that allows you to get the specific object fields without a "Select" method.
Code taken from https://stackoverflow.com/questions/54549506/select-only-specific-fields-with-linq-ef-core
public class MyTestObject
{
public string FirstProperty { get; set; }
public string SecondProperty { get; set; }
public string ThirdProperty { get; set; }
}
IQueryable<MyTestObject> myTestObjects = new List<MyTestObject>()
{
new MyTestObject { FirstProperty = "a1", SecondProperty = "a2", ThirdProperty = "a3", },
new MyTestObject { FirstProperty = "b1", SecondProperty = "b2", ThirdProperty = "b3", },
new MyTestObject { FirstProperty = "c1", SecondProperty = "c2", ThirdProperty = "c3", },
}.AsQueryable();
IQueryable<MyTestObject> result = myTestObjects.SelectFields(new List<string>() { "SecondProperty", "ThirdProperty" });
// Output
FirstProperty: null (default), SecondProperty: 'a2', ThirdProperty: 'a3'
FirstProperty: null (default), SecondProperty: 'b2', ThirdProperty: 'b3'
FirstProperty: null (default), SecondProperty: 'c2', ThirdProperty: 'c3'
I hope this library is useful for you, if so please give a star for this repository, thank you :)