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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.