31

I am using Visual Studio 2005 and C# 2.0, and I am trying to split a comma-separated string using the string.Split function and a lambda expression as follows:

string s = "a,b, b, c";
string[] values = s.Split(',').Select(sValue => sValue.Trim()).ToArray();

I get an error saying that the expression is not recognized -- how can I resolve this?

Donut
  • 110,061
  • 20
  • 134
  • 146
user1292656
  • 2,502
  • 20
  • 48
  • 68

6 Answers6

48

.NET 2.0 does not support LINQ - SO thread;
But you can create a 3.5 project in VS2005 - MSDN thread

Without lambda support, you'll need to do something like this:

string s = "a,b, b, c";
string[] values = s.Split(',');
for(int i = 0; i < values.Length; i++)
{
   values[i] = values[i].Trim();
}
Community
  • 1
  • 1
Jon G
  • 4,083
  • 22
  • 27
11

.NET 2.0 does not use lambda expressions. You need to compile to .NET 3.0 to use them.

basher
  • 2,381
  • 1
  • 23
  • 34
  • 3
    3.5 if he wants to use [`Select`](http://msdn.microsoft.com/en-us/library/bb548891(v=vs.90).aspx) – tnw Jul 10 '13 at 13:29
  • 3
    It's not a matter of .NET supporting lambda expressions. It's a matter of C# 2 not supporting lambda expressions. It's perfectly feasible to use lambda expressions in code targeting .NET 2.0, so long as you build with a C# 3+ compiler. And there are LINQ to OBjects implementations available for .NET 2 as well. – Jon Skeet Jul 10 '13 at 13:31
  • @JonSkeet Very good point, Jon -- I guess we aren't really answering his question. Thanks for the clarification! – tnw Jul 10 '13 at 13:32
  • As Jon said, C# 2.0 is the problem, *not* .NET 2.0. Lambda expressions are a feature of the language, not the runtime. – dss539 Jul 10 '13 at 13:45
6

A way to do this without Linq & Lambdas

string source = "a,b, b, c";
string[] items = source.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • except, I think you then lose spaces within elements, so splitting "a b, c d" would give you "a", "b", "c", "d" rather than "a b", "c d" – Jon G Jul 10 '13 at 13:49
3

Lamba expression aren't included in c# 2.0

maybe you could refert to this post here on SO

Community
  • 1
  • 1
Rémi
  • 3,867
  • 5
  • 28
  • 44
3

Split a Textbox value separated by comma and count the total number of values in text and splitted values are shown in ritchTextBox.

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "";
        richTextBox1.Text = "";

        string strText = textBox1.Text.Trim();
        string[] strArr = strText.Split(',');
        int count = 0;
        for (int i = 0; i < strArr.Length; i++)
        {
            count++;
        }
        label1.Text = Convert.ToString(count);
        for (int i = 0; i < strArr.Length; i++)
        {
            richTextBox1.Text += strArr[i].Trim() + "\n";
        }
    }
remudada
  • 3,751
  • 2
  • 33
  • 60
2

You could use LINQBridge (MIT Licensed) to add support for lambda expressions to C# 2.0:

With Studio's multi-targeting and LINQBridge, you'll be able to write local (LINQ to Objects) queries using the full power of the C# 3.0 compiler—and yet your programs will require only Framework 2.0.

Ryan Weir
  • 6,377
  • 5
  • 40
  • 60
  • He is using Visual Studio 2005 and therefore does not have access to the C# 3.0 compiler, and therefore can't compile lambda expressions no matter what. – dss539 Jul 10 '13 at 13:46