Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

Generics and FindAll for SqlCommand ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lucius - 16 Jun 2007 18:48 GMT
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.
Jon Skeet [C# MVP] - 16 Jun 2007 19:19 GMT
> I am building a generic SqlCommand builder and want to use 2.0
> Generics.
[quoted text clipped - 14 lines]
>    
> 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
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

WenYuan Wang [MSFT] - 18 Jun 2007 09:13 GMT
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.
Peter Duniho - 16 Jun 2007 19:32 GMT
> [...]
> I'm having a problem with the syntax to make this happen,
[quoted text clipped - 6 lines]
>     }
> 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
WenYuan Wang [MSFT] - 20 Jun 2007 12:41 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.