2

I am using the Linq.Dynamic Library and EF6. I am attempting to select only the fields from a database that my user selects.

However, all that is returned is a List<object>, I have attempted to Cast<dynamic>, and every way I can think of, but no matter what the object has 0 fields.

I have also tried explicitly declaring as an IEnumerable and that too was unsuccessful, and was unable to call .ToList(), without first calling Cast<T> which too was unsuccessful.

When converting one of the objects to string I get: "{filenumber=345400, custom2=, custom3=, custom6=4076995332, custom8=4072121417}".

The data is being returned I simply cannot cast it to the appropriate type.

var query = cmax.dbases
    .Where(w => statuses.Any(a => w.statusname == a) && portfolios.Any(a => w.portfolio == a))
    .Select(string.Format("new ({0})", string.Join(",", fields)))
    .Take(Math.Min((int) takeAmount, count - taken));

var take = await query.ToListAsync();

take.ForEach(data => {
    var type = take.GetType();
    var properties = type.GetProperties();
    var propCount = properties.Count();
    properties.ToList().ForEach(prop => {
        var name = prop.Name;
    });
});
Adam Reed
  • 229
  • 4
  • 16
  • @MUG4N I am using Linq.Dynamic, that does not apply, thanks tho. – Adam Reed Apr 29 '15 at 18:08
  • @AdamReed, have you looked at http://stackoverflow.com/questions/1465700/system-linq-dynamic-select-new-into-a-listt-or-any-other-enumerabl – Alex Apr 29 '15 at 18:12
  • @EugenePodskal I appreciate your effort! – Adam Reed Apr 29 '15 at 18:12
  • 1
    @AdamReed That is just strange that they decided to use parentheses instead of curly braces like it is used in C# for anonymous objects. Perhaps it was done exactly to avoid need to escape the braces. – Eugene Podskal Apr 29 '15 at 18:14
  • @Alex Yes, as you can see, I use Reflection here, and am returned with 0 properties. I have no idea what option 2 is. As to option 3, I am using the Library as provided on NuGet and would like to seek an option that doesn't require me expanding on the Library. I believe there should be an easy method to `Cast` the object beign returned, that I am overlooking. – Adam Reed Apr 29 '15 at 18:16

2 Answers2

1

In one of my use cases I have converted the results to List<string[]> through an extension for IQueryable. As I know my column order in Select("New (columnNames...) "), I could easily figure out which one is which.

so here is the code of the extension

public static IList<string[]> ToStringArray(this IQueryable queryFinal)
{
  var query = (IQueryable<dynamic>)queryFinal;
  IList<dynamic> data = query.ToList();
  System.Reflection.PropertyInfo[] props = null;
  if (data.Count > 0) props = data[0].GetType().GetProperties();
  if (props == null) return new List<string[]>();
  /*I do other things using reflection here*/
  return data.Select(d => props.Select(p => (p.GetValue(d, null) ?? string.Empty).ToString()).OfType<string>().ToArray()).ToList();
}

use it as

var result = query.ToStringArray();
pjobs
  • 1,247
  • 12
  • 14
0

The only viable solution I found was to manually parse the data in the string:

take.ForEach(data =>
{
    var dbtrData = new DebtorData();
    var cleaned = data.ToString().Replace("{", "").Replace("}", "");
    var pairs = cleaned.Split(',');
    pairs.ToList().ForEach(pair =>
    {
        var key = pair.Split('=')[0].Replace(" ", "");
        var value = pair.Split('=')[1];
        //USEDATA
    }
});
Adam Reed
  • 229
  • 4
  • 16