Lambda expression select and add

I am trying to select and add to list using lambda.can some body help me where I am making mistake.Below code gives me compile error.

string Param = "param1=test;param2=test2;param3=test3";
List<SqlParameter> param=new List<SqlParameter>();
var ef = d.Select(x => x.Split('='))
          .Select(y => new { id = y[0], val = y[1] })
          .Select(z=>sqlparam.Add(new SqlParameter(z.id, (object)z.val)));
Jon Skeet
people
quotationmark

It's not clear why you're trying to use Select for this, but it's not working because List<T>.Add has a void return method, and the lambda expression you use in a projection has to return the value you're projecting to. I suggest you use:

string param = "param1=test;param2=test2;param3=test3";
var parameters = d.Select(x => x.Split('='))
                  .Select(y => new { id = y[0], val = y[1] })
                  .Select(z => new SqlParameter(z.id, (object)z.val)))
                  .ToList();

(Note how spreading the statement over multiple lines also makes it much simpler to read than having one very long line.)

people

See more on this question at Stackoverflow