none
search ArrayList using Linq for a partial value RRS feed

  • Question

  • Hi,

    I want to search a String ArrayList for a partial value.  have following;

    private string FilenameExists(ArrayList al, string filenameToSearch)
    {
        string retval = string.Empty;

        var fname = from string f
                            in al
                            where f.IndexOf(filenameToSearch) > 0
                            select f.FirstOrDefault();

        retval = fname.ToStrig();


                return retval;
    }

    I want to achieve

        foreach (string str in al)
        {
            if (str.IndexOf(filenameToSearch) > 0)
            {
                retval = str;
                break;
            }
        }
     
    using Linq query.

    How can i do it please?

    Thank you.


    piyush varma
    Wednesday, September 14, 2011 1:04 AM

Answers

  • As written, your query returns the first character of each matching string. The use of the 'var' keyword hides the fact that fname is an IEnumerable<char>.

    If you call FirstOrDefault on the result of the query, you'll get the first matching string:

    private string FilenameExists(ArrayList al, string filenameToSearch)
    {
            return (from string f in al
                                    where f.IndexOf(filenameToSearch) > 0
                                    select f).FirstOrDefault();
    }
    

     

    • Marked as answer by piyush varma Wednesday, September 14, 2011 10:58 PM
    Wednesday, September 14, 2011 9:33 AM

All replies

  • What problems are you having?

    I can see that you are selecting the first character of the string being tested when the string might be null and unecessarily calling ToString() on the result when it can potentially be null.

    See if this works for you:

    .Cast<string>().FirstOrDefault(f => f.IndexOf("w") > 0)
    private string FilenameExists(ArrayList al, string filenameToSearch)
    {
        return al.Cast<string>().FirstOrDefault(f => f.IndexOf(filenameToSearch) > 0);
    }
    
    

    Paulo Morgado
    Wednesday, September 14, 2011 1:28 AM
  • I would use:

          retval = al.Cast<string>().FirstOrDefault(f => f.Contains(filenameToSearch));

     

    Note that you'd be better off using List<string> instead of ArrayList.  In general, it's basically always better to use the collections in System.Collections.Generic over the ones in System.Collections.  Consider replacing your ArrayList with List<string>.  If you do, the above can be written simply as:

        retval = al.FirstOrDefault(f => f.Contains(filenameToSearch));

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, September 14, 2011 2:31 AM
    Moderator
  • As written, your query returns the first character of each matching string. The use of the 'var' keyword hides the fact that fname is an IEnumerable<char>.

    If you call FirstOrDefault on the result of the query, you'll get the first matching string:

    private string FilenameExists(ArrayList al, string filenameToSearch)
    {
            return (from string f in al
                                    where f.IndexOf(filenameToSearch) > 0
                                    select f).FirstOrDefault();
    }
    

     

    • Marked as answer by piyush varma Wednesday, September 14, 2011 10:58 PM
    Wednesday, September 14, 2011 9:33 AM
  • Louis,

    I would say that your answerer is the more LINQy as it uses C# language LINQ constructs.

    That being said, the whole API (even the methods that don't have language constructs like FirstOrDefault) falls under the LINQ umbrella.


    Paulo Morgado
    Wednesday, September 14, 2011 10:07 AM
  • I would say that your answerer is the more LINQy as it uses C# language LINQ constructs.

    That being said, the whole API (even the methods that don't have language constructs like FirstOrDefault) falls under the LINQ umbrella.

    My reply was not meant to be picky about the definition of Linq, but to explain what was wrong in the OP's query.

    /picky mode on

    That being said, I'd argue that Linq means "Language integrated query". The System.Linq namespace is not Linq. It is used by Linq. You can use Linq without using any object in that namespace. You don't even need a reference to System.Core.dll.

    Wednesday, September 14, 2011 10:25 AM
  • Louis,

    I did not inteded to be picky nor thing you entered picky mode.

    When I meant API I meant the methods that the compiler is expecting to translate the keywords to, not a particular implementation.

    Using the C# 3.0 compiler (the one where LINQ and extension methods appeared) I successfully wrote code to run on .NET 2.0 RTM, just by implementing the "LINQ operators" that I needed.


    Paulo Morgado
    Wednesday, September 14, 2011 10:32 AM
  • I use a static Where method in the project I'm working on. I use it to generate SQL queries based on attributes on the class, and returning instances of the class.

    That's make me thinking about opening a new thread about strange Linq uses.

    • Edited by Louis.fr Wednesday, September 14, 2011 3:44 PM
    Wednesday, September 14, 2011 3:43 PM
  • /picky mode on

    That being said, I'd argue that Linq means "Language integrated query". The System.Linq namespace is not Linq. It is used by Linq. You can use Linq without using any object in that namespace. You don't even need a reference to System.Core.dll.

    Just to comment on your picky comment:

    While I agree that's what LINQ stands for, the LINQ page, as well as all discussions from Microsoft, use LINQ to refer to the Language Integrated query features, but also the "set, and transform operations" that were added to work with them.  This is effectively the System.Linq namespace.  I think it's perfectly reasonable (and correct) to use the term LINQ to describe the sequence operators defined in the Enumerable and Queryable types as well as the language integrated querying syntax.

     

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, September 14, 2011 4:39 PM
    Moderator
  • I think it's perfectly reasonable (and correct) to use the term LINQ to describe the sequence operators defined in the Enumerable and Queryable types as well as the language integrated querying syntax.

    No problem. I think it's reasonable too. The Linq syntax cannot be used for calling methods like Aggregate or First, but I include them in the Linq concept. My reply was only made in reaction to what I perceived, maybe erroneously, as a comment about me being picky. I should have introduced it with "If I wanted to be picky, I'd say that..."
    Thursday, September 15, 2011 10:28 AM