Store arrays and search

I have a set of Revision Number  corresponding  FileName  and Status
Ex
  R1   R1.pdf     P
   R1  R1.pdf     N
  R1  R1.docx    P
  R2  R2.docx    P
......

 I need to store it  and need to  do search  against a string value as follows
    Search all Revisions in the "storage" to see the  given string  is found  ( Eg. Check if R1 is found)
     For given RevisionNo and FileName and Status , Check if a matching row is found in storage  ( Check if a row with R1  R1.docx    P   is found)

Can you please give me sampe code to achieve this ?
Sam OZAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You can easily accomplish this using LINQ:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29083115
{
    class Program
    {
        readonly static List<dynamic> storage = new List<dynamic>
        {
            new { RevisionNumber = "R1", FileName = "R1.pdf", Status = "P" },
            new { RevisionNumber = "R1", FileName = "R1.pdf", Status = "N" },
            new { RevisionNumber = "R1", FileName = "R1.docx", Status = "P" },
            new { RevisionNumber = "R2", FileName = "R2.docx", Status = "P" }
        };

        static void Main(string[] args)
        {
            Console.WriteLine("Revision only matches -");
            // If you just want to match revisions
            var revision_matches = (from document in storage
                                    where document.RevisionNumber.Equals("r1", StringComparison.OrdinalIgnoreCase)
                                    select document);

            foreach (var revision in revision_matches)
            {
                Console.WriteLine(revision);
            }

            Console.WriteLine();
            Console.WriteLine("Revisions that match a status of P -");
            // If you want to get revisions of a certain status
            revision_matches = (from document in storage
                                where document.RevisionNumber.Equals("r1", StringComparison.OrdinalIgnoreCase) &&
                                document.Status.Equals("p", StringComparison.OrdinalIgnoreCase)
                                select document);

            foreach (var revision in revision_matches)
            {
                Console.WriteLine(revision);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
Sam OZAuthor Commented:
Thanks.But My idea is to get an array like storage which I need to use in the loop
0
it_saigeDeveloperCommented:
A list *is* an array like structure that can be used in a loop.

Need proof?
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29083115
{
    class Program
    {
        readonly static List<dynamic> storage = new List<dynamic>
        {
            new { RevisionNumber = "R1", FileName = "R1.pdf", Status = "P" },
            new { RevisionNumber = "R1", FileName = "R1.pdf", Status = "N" },
            new { RevisionNumber = "R1", FileName = "R1.docx", Status = "P" },
            new { RevisionNumber = "R2", FileName = "R2.docx", Status = "P" }
        };

        static void Main(string[] args)
        {
            Console.WriteLine("Revision only matches -");
            // If you just want to match revisions
            for (int i = 0; i < storage.Count(); i++)
            {
                if (storage[i].RevisionNumber.Equals("r1", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine(storage[i]);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Revisions that match a status of P -");
            // If you want to get revisions of a certain status
            foreach (var revision in storage)
            {
                if (revision.RevisionNumber.Equals("r1", StringComparison.OrdinalIgnoreCase) && revision.Status.Equals("p", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine(revision);
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Gives the same output as above...  At it's simplest, LINQ is another method for querying data in order to retrieve subsets.  And since an array is, by nature, an enumerable, you can use LINQ on arrays.

Proof of concept -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29083115
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var evens = (from n in numbers where n % 2 == 0 select n);
            for (int i = 0; i < evens.Count(); i++)
            {
                Console.WriteLine(evens.ToList()[i]);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG
-saige-
0
Sam OZAuthor Commented:
Thanks. My quick attempt could not convert this into VB.NET code . Can you please give the vb.net code?
0
it_saigeDeveloperCommented:
Sure thing.  Here is a VB.NET version of all of the solutions provided above:
Module Module1
	ReadOnly storage As New List(Of Object) From
	{
		New With {.RevisionNumber = "R1", .FileName = "R1.pdf", .Status = "P"},
		New With {.RevisionNumber = "R1", .FileName = "R1.pdf", .Status = "P"},
		New With {.RevisionNumber = "R1", .FileName = "R1.docx", .Status = "R"},
		New With {.RevisionNumber = "R2", .FileName = "R1.docx", .Status = "R"}
	}

	Sub Main()
		Console.WriteLine("Revision only matches -")
		'' If you just want to match revisions
		Dim revision_matches = From document In storage
							   Where document.RevisionNumber.ToString().Equals("r1", StringComparison.OrdinalIgnoreCase)
							   Select document

		For Each revision In revision_matches
			Console.WriteLine(revision)
		Next

		Console.WriteLine()
		Console.WriteLine("Revisions that match a status of P -")
		'' If you just want to get revisions of a certain status
		revision_matches = From document In storage
						   Where document.RevisionNumber.ToString().Equals("r1", StringComparison.OrdinalIgnoreCase) AndAlso
							   document.Status.ToString().Equals("p", StringComparison.OrdinalIgnoreCase)
						   Select document

		For Each revision In revision_matches
			Console.WriteLine(revision)
		Next

		Console.WriteLine()
		Console.WriteLine("Revision only matches -")
		'' If you just want to match revisions
		For i As Integer = 0 To storage.Count() - 1
			If storage(i).RevisionNumber.ToString().Equals("r1", StringComparison.OrdinalIgnoreCase) Then
				Console.WriteLine(storage(i))
			End If
		Next

		Console.WriteLine()
		Console.WriteLine("Revisions that match a status of P -")
		'' If you just want to get revisions of a certain status
		For Each revision In storage
			If revision.RevisionNumber.ToString().Equals("r1", StringComparison.OrdinalIgnoreCase) _
				AndAlso revision.Status.ToString().Equals("p", StringComparison.OrdinalIgnoreCase) Then
				Console.WriteLine(revision)
			End If
		Next

		Console.WriteLine()
		Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
		Dim evens = From n In numbers Where n Mod 2 = 0 Select n
		For i As Integer = 0 To evens.Count() - 1
			Console.WriteLine(evens.ToList()(i))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.