locked
How Entity Framework can return data table instead of List RRS feed

  • Question

  • See this query

    using (var context = new SchoolDBEntities())
    {
        var query = context.Students
                           .where(s => s.StudentName == "Bill")
                           .FirstOrDefault<Student>();
    }


    using (var context = new SchoolDBEntities())
    {
        var query = context.Students
                           .where(s => s.StudentName == "Bill")
                           .ToList();
    }

    the above query return list but how could i create one extension which i can use in the place of ToList() to return data table.

    please help me with extension routine which return data table instead of ToList().

    https://www.codeproject.com/Articles/5263745/Return-DataTable-Using-Entity-Framework i see this article code but still not able to create extension which i can use to return data table instead of List.

    thanks

    Tuesday, September 15, 2020 10:59 AM

Answers

  • Hi Sudip_inn
    First, Entity Framework is geared towards returning entities and isn't meant to return DataTable objects.
    And if you want to return DataTable using Entity Framework, you can convert a list to a DataTable.
    Vishal Sharma has provided a code example to use extension method that converts generic list To Datatable.
    And you can also refer to this link.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 16, 2020 6:16 AM
  • Hi Sudip_inn,
    If you want to return a DataTable directly, you can use straight ADO.NET instead.
    More discussion in this thread you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, September 17, 2020 4:56 PM
    Wednesday, September 16, 2020 9:54 AM

All replies

  • Hi Sudip_inn
    First, Entity Framework is geared towards returning entities and isn't meant to return DataTable objects.
    And if you want to return DataTable using Entity Framework, you can convert a list to a DataTable.
    Vishal Sharma has provided a code example to use extension method that converts generic list To Datatable.
    And you can also refer to this link.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 16, 2020 6:16 AM
  • Sir, ToList() populate data into list but if i use ToDataTable() extension it will convert list to data table. when there would be huge data then there would performance hit i assume. can't we instruct EF to populate data in data table instead of list?

    looking for your further suggestion. thanks

    Wednesday, September 16, 2020 9:22 AM
  • Hi Sudip_inn,
    If you want to return a DataTable directly, you can use straight ADO.NET instead.
    More discussion in this thread you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, September 17, 2020 4:56 PM
    Wednesday, September 16, 2020 9:54 AM