views:

75

answers:

1

I have a WinForm App that uses multiple DataGridViews. The DGV's used to be bound to DataTables in the old SProc's DAL. As I was converting the SProcs to LINQ I originally was following suite but I am wondering if that is the "best" way.

This is a sample of what I am doing.

        internal static CmoDataContext context = new CmoDataContext();

        public static DataTable GetAllMembers(Guid workerID)
    {
        DataTable dataTable;
        using (context)
        {
            var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               select
                                   new
                                       {
                                           enrollment.ADRCReferralID,
                                           enrollment.ClientID,
                                           enrollment.CMONurseID,
                                           enrollment.CMOSocialWorkerID,
                                           enrollment.DisenrollmentDate,
                                           enrollment.DisenrollmentReasonID,
                                           enrollment.EconomicSupportWorkerID,
                                           enrollment.EnrollmentDate
                                       };

            dataTable = AllEnrollees.CopyLinqToDataTable();
        }
        return dataTable;
    }

CopyLinqToDataTable() is a custom class that, obviously, converts an IEnumerable<T> to a DataTable.

Is there anything wrong with doing it this way? Should I be using IQueryable?

Thanks

+2  A: 

I don't see anything wrong with doing it the way you are doing. Converting from an IEnumberale list to a DataTable. If you are working on a framework you should be able to change the internal workings without effecting any methods calling it.

You could write your Linq query to return you a datatable instead of a list, but I don't think it would save you in processing time either way.

Just so you know a DataGrid can bind to a List, so if you wanted to take the effort of just binding it to the List I would do that. However if you don't have the time or it is out of the scope I see nothing wrong with waiting until later.

David Basarab
+1 yea, I just bind my DGVs to IList<>
dotjoe
Won't the DataGridView bind directly to the Linq to SQL graph via the IListSource interface?
Lazarus