0

New to linq and trying to acheive

  select * from tblCountries order by CountryName

I followed the procedre in http://msdn.microsoft.com/en-us/library/bb669096%28v=vs.110%29.aspx, and can now access the CopyToDataTable method.

But now there is a syntax error at the orderby part-

cannot convert IOrderedQueryable<Context.tblCountry> to Ienumerable<system.data.datarow>

code

public DataTable GetAllRecords(bool bDeleted)
{
  IEnumerable<DataRow> query = (from p in MedianDB.tblCountries
                              select p).OrderBy(p => p.CountryName);//error here                                        

    DataTable dt = query.CopyToDataTable<DataRow>();

    if (dt != null)
        return dt;
    else
        return dt;
}
Ruby
  • 949
  • 7
  • 31
  • 68
  • 1
    Check this out: http://stackoverflow.com/questions/1595350/why-am-i-not-getting-copytodatatable-in-linq-query – Aage Oct 28 '13 at 11:23
  • What is `MedianDB`? If it's a strongly typed `DataSet` it should work, otherwise not. – Tim Schmelter Oct 28 '13 at 11:25
  • @bump.Thank You, I came here after that but could not comprehend. – Ruby Oct 28 '13 at 11:32
  • @Tim, I used an data entity model, and MedianDB is the name of the model – Ruby Oct 28 '13 at 11:33
  • 3
    As the comments already suggested, CopyToDataTable only works if your query returns an IEnumerable where T:DataRow. In your case, it is not, it is an Entity, so here is another suggestion: How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow http://msdn.microsoft.com/en-us/library/bb669096(v=vs.110).aspx – Sadek Noureddine Oct 28 '13 at 12:25
  • @Sadek I followed the method in the link and now can access the copytodatatable method, but now there is a syntax error in linq. I have updated my question.Thank you – Ruby Oct 29 '13 at 10:00
  • Put the errors/exceptions you are getting in your question. – Sadek Noureddine Oct 29 '13 at 10:05

0 Answers0