c# linqToExcel covert to string objects

Hi Guys,

I have a NUnit test in which I'm trying to read the testdata from rows in an excel file. My NUnit test is quite simple for this example and is below

 [Test, TestCaseSource("linqTesting2")]
        public void CompleteFormTestDataSource4(String username, String password)
        {

            LoginPageObject pageLogin = new LoginPageObject();
            EAPageObject pageEA = pageLogin.Login(username, password);

        }

Open in new window


My method to read the excel rows using linqToExcel is below

 
public static IQueryable<LinqToExcel.Row> linqTesting2()
        {
            var excel = new ExcelQueryFactory(Constants.testDataPath4);
            var testcases = from c in excel.Worksheet("Sheet1")
                            where c["UserName"] == "SonjaB"
                            select c;
            return testcases;
        }

Open in new window


To use the row data in my unit test do I need to somehow convert it to a IEnumerable<string[]> before it gets returned to the test. If so any ideas on how to do this would be appreciated. When I ran the test the only error I get is I need to pass the correct amount of parameters.

Thanks
victoriaharryAsked:
Who is Participating?
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.

Karrtik IyerSoftware ArchitectCommented:
So you want all the column values of a excel row to be converted to an array of string?
victoriaharryAuthor Commented:
Yes, so each row of data can be passed into the NUnit test
Karrtik IyerSoftware ArchitectCommented:
Something like below.
 var testcases = (from Row r in excel.Worksheet("Sheet1")
                where r["UserName"] == "SonjaB"
                select r.ToArray()).AsEnumerable();
            IEnumerable<String[]> values = (from cells in testcases
                select cells.Select(x => x.Value.ToString()).ToArray()).AsEnumerable();

Open in new window

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Karrtik IyerSoftware ArchitectCommented:
You can combine into a single statement as well, like below:
       IEnumerable<string[]> testcases = (from Row r in excel.Worksheet("Sheet1")
                            where r["UserName"] == "SonjaB"
                            select r).AsEnumerable().Select( x => x.ToArray().Select( y => y.Value.ToString()).ToArray());

Open in new window

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Based on what I'm seeing in the documentation, you should be creating an anonymous object that has each piece of data expected by your parameters:

public static IEnumerable<object> linqTesting2()
{
    var excel = new ExcelQueryFactory(Constants.testDataPath4);
    var testcases = from c in excel.Worksheet("Sheet1")
                    where c["UserName"] == "SonjaB"
                    select new
                    {
                        username = c["UserName"].Value,
                        password = c["Password"].Value
                    };
                    
    return testcases;
}

Open in new window


It appears that you add the properties to the anonymous object in the order that the test method lists parameters. My guess is that NUnit uses reflection to loop over the object properties, but I left the property names the same as the parameter names (including casing) just in case.

You also don't need AsEnumerable, ToArray, or any other similar method. The result of a LINQ query is already IEnumerable, so just go with that. You will note that I changed the return type of the factory method.
victoriaharryAuthor Commented:
Thanks for the responses guys. When I run the test in Test Explorer using either of the responses above I get the error "Not enough arguments provided, provide at least two arguments. When I build the solution I can see the test explorer is creating the correct amount of tests and I can see in test explorer the data next to the test is from the excel file rows. However when I ran one of the tests I get the error. The test I'm running is below

[Test, TestCaseSource("linqTesting2")]
        public void CompleteFormTestDataSource4(String username, String password)
        {

            LoginPageObject pageLogin = new LoginPageObject();
            EAPageObject pageEA = pageLogin.Login(username, password);

        } 

Open in new window

victoriaharryAuthor Commented:
I just noticed the data being sent back to the test is {username = SonjaB, password = mypassword} instead of {SonjaB,myPassword}
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
You can try using an object array instead.

var testcases = from c in excel.Worksheet("Sheet1")
                where c["UserName"] == "SonjaB"
                select new[]
                {
                    c["UserName"].Value,
                    c["Password"].Value
                };

Open in new window

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
C#

From novice to tech pro — start learning today.