none
Return a DataRow from a DataTable using LINQ To SQL RRS feed

  • Question

  • I have a DataTable named ProjectMasters which has a primary ID column named projectID. I am passing a variable named ProjectID to a function that should return a DataRow, but intellisense puts a red squiggly line under "where" and next to "int".

    public DataRow GetProject(int ProjectID)
    {
        DataRow result = (from row in ProjectMasters.AsEnumerable() 
                        where row.Field(int)("projectID") == ProjectID
                        select row).SingleOrDefault;
        return result as DataRow;
    }
    How should I alter the query?


    Rob E.


    • Edited by REvans611 Friday, February 27, 2015 8:53 PM
    Friday, February 27, 2015 8:52 PM

Answers

  • Hello Rob,

    Please have a try with below query:

    DataRow result = (from row in dt.AsEnumerable() 
    
                        where row.Field<int>("OrderID") == 1
    
                        select row).SingleOrDefault();
    

    The DataRowExtensions.Field<T> Method (DataRow, String) is a generic method which use the angle brackets in C# language.

    If this is not work, please share the exception message with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 2, 2015 2:39 AM
    Moderator