How to return either all properties or their selection in C # using LINQ?

I have a method that accepts a lambda expression indicating which columns to select from a table MyTable.

public List<T> GetSelection<T>(string a, Expression<Func<MyTable, T>> columnsToSelect = null)
{
    IQueryable<MyTable> query = _myTableRepository.Where(c => c.Name == a);

    if (columnsToSelect != null)
        return query.Select(columnsToSelect).ToList();

    return query.ToList(); // Intellisense gives an error here, please read on
}

As you can see, the value columnsToSelecthas a default value null, which allows me to call this method in any of the following two ways:

// passing in a lambda expression to select the columns I want
var result = GetSelection("AValue", t => new { t.Prop1, t.Prop2 }); 

// or like this, in which case I want all the columns
var result = GetSelection("AValue");

The error I get from intellisense is this:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

-, , . , , MyTable , , , MyTable. ?

, , , , .

+4
3

:

...

var result = GetSelection("AValue");

, . , T . - . :

public List<T> GetSelection<T>(
    string a, Expression<Func<MyTable, T>> columnsToSelect)
{
    return _myTableRepository.Where(c => c.Name == a).Select(columnsToSelect)
        .ToList();
}

public List<MyTable> GetSelection<MyTable>(string a)
{
    return GetSelection(a, t => t);
}
+3

, :

if (columnsToSelect != null)
{
    var result = query.Select(s => s.GetType().GetProperty("columnName").GetValue(s, null).ToString())
                      .Select(s => new MyTable()
                      {
                          YourProperty = s.ToString()
                      }).ToList();
}
+1

If you can define an interface for your table

public interface IMyTable<T>
{
    string Name { get; set; }
    T GetAll();
}

then this would compile

public List<T> GetSelection<T>(string a, Expression<Func<IMyTable<T>, T>> columnsToSelect = null)
{
    IQueryable<IMyTable<T>> query  = (IQueryable<IMyTable<T>>) _myTableRepository.Where(c => c.Name == a);

    if (columnsToSelect == null)
        columnsToSelect = (Expression<Func<IMyTable<T>, T>>)(table => (table.GetAll()));

    return query.Select(columnsToSelect).ToList();

}

Demo to use

IMyTable<List<object>> xxx = new MyTable() { Surname = "b", Addresss = 1 };
xxx.Name = "a";
IMyTable<List<object>> yyy = new MyTable() { Surname = "d", Addresss = 2 };
yyy.Name = "c";
var repo = new List<IMyTable<List<object>>>() { xxx, yyy }.AsQueryable();
var Test = new Test<List<object>>(repo);
var generic = Test.GetSelection<List<object>>("c");
var specific = Test.GetSelection<List<object>>("c",
                (Expression<Func<IMyTable<List<object>>, List<object>>>) 
                    (x => new List<object>() { x.Name, ((MyTable)x).Addresss }));
var specifc2Columns = specific
    .Select(rows => new { Name = rows[0], Address = rows[1] });

where is the demo table

 internal class MyTable : IMyTable<List<object>>
 {
     public string Surname { get; set; }
     public int Addresss { get; set; }
     string IMyTable<List<object>>.Name{ get; set; }
     List<object> IMyTable<List<object>>.GetAll()
     {
         return new List<object>() { ((IMyTable<List<object>>)this).Name, Surname, Addresss };
     }
 }
+1
source

Source: https://habr.com/ru/post/1671501/


All Articles