none
c# linq query RRS feed

  • Question

  •  

     

    foreach (DataTable table in HRnetObject.Tables)

    {


    var
    ProductNames = (from row in table

    select row.field<string>("ProductName")).Distinct();

    }

    i am getting an error in Select clause as 'Cannot convert lambda expression to type 'string' because it is not a delegate type '
    How to resolve this issue?


    developer
    Monday, January 19, 2009 12:01 PM

All replies

  • Are you missing a "using System.Linq"?


    Wayward LINQ Lacky
    Monday, January 19, 2009 4:05 PM
    Moderator
  • Try:

    var ProductNames = table.Select(row => row.field<string>("ProductName")).Distinct();

    This is what the compiler does anyways.  If that doesn't work, check and make sure your return types match.
    Tuesday, January 20, 2009 6:20 PM
  • Greeting!!

    Do following:

    var ProductNames = table.AsEnumerable().Select(p => new string
                    {
                        p.Field<string>("ProductName")
                    }).Distinct().ToList();
    

    Thanks & Regards,

    Asma Khalid

    ---------------------------------

    Mark answer if this helps.


    Thanks & regards, Asma Khalid

    Tuesday, November 10, 2015 12:29 PM
  • DataTable is not Enumerable to use it in the <g class="gr_ gr_21 gr-alert gr_spell undefined ContextualSpelling ins-del multiReplace" data-gr-id="21" id="21">linq</g> expressions. You've to make it enumerable.

    table.AsEnumerable();

    So your code should be:

    foreach (DataTable table in HRnetObject.Tables)
    
    {
    
    var ProductNames = (from row in table.AsEnumerable()
    
    select row.Field<string>("ProductName")).Distinct();
    
    }

    To make this work, you've to include assembly reference to System.Data.DataSetExtensions


    Regards, Karthik

    Thursday, November 12, 2015 12:11 PM