c# linqToExcel covert to string objects

victoriaharry
victoriaharry used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Commented:
So you want all the column values of a excel row to be converted to an array of string?

Author

Commented:
Yes, so each row of data can be passed into the NUnit test
Karrtik IyerSoftware Architect

Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Karrtik IyerSoftware Architect

Commented:
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

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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.

Author

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

Author

Commented:
I just noticed the data being sent back to the test is {username = SonjaB, password = mypassword} instead of {SonjaB,myPassword}
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial