-1

I have the following code:

var aaData = myapi.GetData().AsEnumerable().Select(x => new {
                Id = x["myID"],
                Desc = x["myDesc"]                   
            });

Trying to do the following

aaData = aaData.OrderBy((string.Join(",", request.Order
.Select(x => request.Columns[x.Column].Data + " " + x.Dir))));

Getting error:

CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<>' to 'System.Data.EnumerableRowCollection<>'. An explicit conversion exists (are you missing a cast?)

How to fix this?

GetData returns a DataTable

Request is an object having a property:

public OrderCol[] Order { get; set; }

OrderCol is

public class OrderCol {
            public int Column { get; set; }
            public string Dir { get; set; }
        }

Thanks for your assistance.

The above code works for the case when I get a List<> back instead of DataTable. The error states that a Cast is needed and it seems to be how DataTable.AsEnumerable is set up as a EnumerableRowCollection

Can use a mock datatable to mimic the above

DataTable dt = new DataTable();
 dt.clear();
 dt.Columns.Add("myID");
 dt.Columns.Add("myDesc");
N. Kaufman
  • 81
  • 1
  • 2
  • 9
  • 1
    What is `myapi.GetData()`, what is `request`? – Gert Arnold Dec 29 '19 at 19:59
  • 1
    Please give us data structure of 'myapi.GetData()' and 'request' . – Nguyễn Văn Phong Dec 30 '19 at 00:21
  • Sorry, GetData returns a DataTable. – N. Kaufman Dec 30 '19 at 10:16
  • And the `Request`? – Selim Yildiz Dec 30 '19 at 10:24
  • Edited my original post. Hope it now has all the necessary info – N. Kaufman Dec 30 '19 at 11:00
  • Could you pls add working code as well `The above code works for the case when I get a List<> back instead of DataTable`? – Selim Yildiz Dec 30 '19 at 17:49
  • What remains unclear is what kind of method `OrderBy` is. `aaData` is `IEnumerable`, so the expected `OrderBy` method would look like `OrderBy(row => row....)`. – Gert Arnold Dec 31 '19 at 15:58
  • I think it would be helpful to first look at inner select request.Order.Select(x => request.Columns[x.Column].Data + " " + x.Dir). This will output in the form - myID Desc. So for one column sort we would get OrderBY("myID desc"). Does that answer your question? – N. Kaufman Dec 31 '19 at 18:45
  • No, this `OrderBy` is essential. Seemingly, its output is `EnumerableRowCollection`, which you're trying to write to a variable of type `IEnumerable`. When asking a question try to be aware of all details that could matter. Your question is about types, so it's essential to show all types that are involved in any part of the code. – Gert Arnold Jan 01 '20 at 10:36
  • This OrderBy is standard LINQ API so are you asking how LINQ API works? :-) Yes i'm aware of issue being of types, the Error does suggest that, doesn't it and so does the title of the issue. – N. Kaufman Jan 01 '20 at 16:37
  • As I said before, this is *not* the expected LINQ method signature, which would be a lambda expression. – Gert Arnold Jan 01 '20 at 22:10
  • From LINQ public static IEnumerable OrderBy(this IEnumerable source, string ordering, params object[] values); public static IEnumerable OrderBy(this IEnumerable source, string ordering, params object[] values); – N. Kaufman Jan 02 '20 at 01:11
  • perhaps adding a mock DataTable and trying the above will provide you with the info. I've added a code snippet that could help – N. Kaufman Jan 02 '20 at 01:15

1 Answers1

0

all i needed was to convert to a list and things worked fine

N. Kaufman
  • 81
  • 1
  • 2
  • 9