Generics and FindAll for SqlCommand ?

  • Thread starter Thread starter lucius
  • Start date Start date
L

lucius

I am building a generic SqlCommand builder and want to use 2.0
Generics.
I want to create a Generic list of SqlCommands, and put new
SqlCommands
in the list, populate the commands, and then run them. New SqlCommands
should go into the list if a command does not already have the same
CommandText (which maps to a SQL Server stored procedure name).

I'm having a problem with the syntax to make this happen,
here is what I have so far:

List<SqlCommand> sqlCommands = new List<SqlCommand>();
if ( sqlCommands.FindAll( //what to put here... ) == false )
{
// add the new command
}

What is the right way to do this?

Thanks.
 
lucius said:
I am building a generic SqlCommand builder and want to use 2.0
Generics.
I want to create a Generic list of SqlCommands, and put new
SqlCommands
in the list, populate the commands, and then run them. New SqlCommands
should go into the list if a command does not already have the same
CommandText (which maps to a SQL Server stored procedure name).

I'm having a problem with the syntax to make this happen,
here is what I have so far:

List<SqlCommand> sqlCommands = new List<SqlCommand>();
if ( sqlCommands.FindAll( //what to put here... ) == false )
{
// add the new command
}

What is the right way to do this?

Well, you could use an anonymous method. I'd also not use FindAll, but
Exists:

string newCommandText = ...

if (!sqlCommands.Exists(delegate (SqlCommand old)
{ return old.CommandText == newCommandText; }
))
{
// Add the new command
}
 
[...]
I'm having a problem with the syntax to make this happen,
here is what I have so far:

List<SqlCommand> sqlCommands = new List<SqlCommand>();
if ( sqlCommands.FindAll( //what to put here... ) == false )
{
// add the new command
}
What is the right way to do this?

Two things. First, you need a Predicate delegate there. Second,
List<>.FindAll() returns a new List<>, not a boolean. It appears to me
that what you really want is the List<>.Exists() method. For example:

List<SqlCommand> sqlCommands = new List<SqlCommand>();

if (!sqlCommands.Exists(new Predicate<SqlCommand>(
delegate(SqlCommand cmd)
{ return cmd.CommandText == cmdNew.CommandText; })))
{
// add the new command cmdNew
}

where "cmdNew" is a variable containing the reference to the new
SqlCommand instance.

The example uses an anonymous delegate. This isn't strictly necessary,
but without an anonymous delegate you need a class that contains the new
SqlCommand to test against. If you had a class that already included that
(for example, you derive from SqlCommand), you could put the delegate
method in that class and it would just test for equality against itself.
But absent that, the anonymous delegate syntax allows you to capture
whatever local variable or parameter contains the data you're looking to
compare to.

Note: I'm pretty sure the syntax above is right, but I admit that I
sometimes need the compiler to hold my hand when trying to figure out
exactly how to construct the necessary delegate types. :) Hopefully the
above at least gets you headed in the right direction, and my apologies if
it doesn't compile right off the bat.

Pete
 
Hello Lucius,
I agree with Jon and Peter. Exists() is what you really need.
The script will look like the one below:

SqlCommand sc = new SqlCommand(....);

string newCommandText = sc.CommandText;
if (!sqlCommands.Exists(delegate(SqlCommand old)
{ return old.CommandText == newCommandText; }))
{
sqlCommands.Add(sc);
}

BTW, in C# and Visual Basic, it is not necessary to create the
Predicate<string> delegate (Predicate(Of String) in Visual Basic)
explicitly. These languages infer the correct delegate from context and
create it automatically.
http://msdn2.microsoft.com/en-us/library/bfed8bca.aspx
[List.Exists Method]

Another way, you may look into each command in lLst and check whether the
command has been inserted already.

private bool exist(List<SqlCommand> sqlCommands,SqlCommand sqlCommand)
{
foreach (SqlCommand scd in sqlCommands)
{
if (scd.CommandText == sqlCommand.CommandText)
return true;
}
return false;
}

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Lucius,

I haven't heard from you a couple of days. Have you resolved the issue so
far?
If you still have anything unclear or anything we can help with, please
feel free to update here. I'm glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top