none
LINQ to SQL-pubs Console application: 3 errors in Lambda Expression - Why? How to solve them? RRS feed

  • Question

  • Hi all,

    In my Visual C# 2008 Express, I created a LINQ to SQL-pubs Console application with the following code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.ComponentModel;
    using System.Data;
    
    namespace LinqLambdaConsoleLee442
    {
        class Program
        {
            static void Main(string[] args)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
                //---loads the Authors table into dataset---
                conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;" +
                      "Initial Catalog=pubs;Integrated Security=True");
                comm = new SqlCommand("SELECT * FROM Authors", conn);
                adapter = new SqlDataAdapter(comm);
                adapter.Fill(ds);
                var query =
                    ds.Tables[0].AsEnumerable()
                    .Count (a => a.Field<string("State") == "CA");
                        Console.WriteLine(query);
                Console.ReadLine();
               
            }
        }
    }
    

    I got the following 3 errors immediately:

    Error 1 'System.Data.EnumerableRowCollection<System.Data.DataRow>' does not contain a definition for 'Count' and the best extension method overload 'System.Linq.Enumerable.Count<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LinqLambdaConsoleLee442\LinqLambdaConsoleLee442\Program.cs 29 17 LinqLambdaConsoleLee442
    Error 2 Argument '2': cannot convert from 'lambda expression' to 'System.Func<System.Data.DataRow,bool>' C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LinqLambdaConsoleLee442\LinqLambdaConsoleLee442\Program.cs 29 25 LinqLambdaConsoleLee442
    Error 3 Invalid expression term 'string' C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LinqLambdaConsoleLee442\LinqLambdaConsoleLee442\Program.cs 29 44 LinqLambdaConsoleLee442

    Please help and tell me why I got these three errors and how I can solve them.

    Thanks,
    Scott Chang 

     

    Tuesday, November 24, 2009 12:55 PM

Answers

  • Hi,

    If it comes to the query, I found a small error. It should look like:

     var query =
                    ds.Tables[0].AsEnumerable()
                    .Count(a => a.Field<string>("State") == "CA");
    Best Regards
    • Marked as answer by Scott_Chang Wednesday, November 25, 2009 12:29 PM
    Wednesday, November 25, 2009 10:19 AM