Parsing API Call data

Hello Experts,
I have an exiting program in C# which call an API.  The API returns more than 100 rows.  The C# program looks as below.

var memberFound = MemberSearch(Date);
if (memberFound != null)
	foreach (var member in memberFound)
	{
	    var list = new List<string>();
	    list.Add(member.memberNumber);
	}

Environment.Exit(0);

Open in new window


I am trying to parse the data in stored temporary in the member variable (or memberFound) which contains the following information.  The data stored looks like an Array, but not works like that.  I am trying to pass (member or memberFound) data to another method where I can parse the data and Insert to a table.

Here I have hard time understand var data type.  Please try to help.

memberCode = 1001                        
age = 32
name = Mark John
memberType = "xxx"
balanceAmount = 2450.00
.
.
.

Open in new window


Thank you very much in advance.
RadhaKrishnaKiJayaAsked:
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.

Chris StanyonWebDevCommented:
var is used to implicitly declare a variable type, so the compiler effectively tries to guess the type of your variable. When you use var, you don't need to know the return type of a method. This can sometimes be a good thing and other times a bad thing! Looking at your code, we have no idea what datatype memberFound or member actually is.

A quick example: In your code, you are implicitly declaring the variable type:

var list = new List<string>();

This is the same as explicitly declaring the variable:

List<string> list = new List<string>();

Without us knowing what member is, we can't really give you a solution. Judging by the fact that you call member.memberNumber, I'm guessing it's an object of some type.

Your code does look a little odd as you're declaring a new list in each iteration of your loop. It makes more sense to declare the list outside the loop and then add to it inside the loop:

var memberFound = MemberSearch(Date);
if (memberFound != null) {
    var list = new List<string>();

    foreach (var member in memberFound)
    {
         list.Add(member.memberNumber);
    }
}

Open in new window

RadhaKrishnaKiJayaAuthor Commented:
Chris Stanyon, Thank you!

I agree that the code looks odd.  In fact this is not the whole code.  After this, another call is made to get the memberDetails.  This code is from a third party.  Please advice how to make it better.

The Data is like this
--------------------------------------
memberCode = 1001        - Integer                
age = 32                            - Integer
name = Mark John            - String
memberType = "xxx"         - String
balanceAmount = 2450.00 - Money

Now the question is, what is the best way to write this raw data into a table.  Please advice.

Thank you again for your help.
Chris StanyonWebDevCommented:
How you write it to a table will depend largely on how you've structured your app, particularly the data layer, so I can't give you a specific example. If you're wanting to insert all the member info into your table, then why not just pass in the results of the MemberSearch() method:

var membersFound = MemberSearch(Date);
InsertIntoYourDB(membersFound);

You'd need to write your InsertIntoYourDB() method to accept the results of the MemberSearch, and use that to insert into your DB however you've set up your data layer.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

RadhaKrishnaKiJayaAuthor Commented:
How my method should look like?  I am trying as below.  But getting errors.

private void InsertIntoYourDB(var membersFound)
{
}

Error      CS1503      Argument 1: cannot convert from 'System.Collections.Generic.IList<MemberAPIApp.Program.Memo>' to 'var'      MemberManager      
Error      CS0825      The contextual keyword 'var' may only appear within a local variable declaration or in script code      MemberManager
Chris StanyonWebDevCommented:
You can only use var to declare a variable in the local scope. You're trying to use it to declare the argument type. For that, you'll need to explicitly declare it. In cases like this, it gives you more flexibility if you use a general interface such as IList or IEnumerable (depending on your needs):

private void InsertIntoYourDB(IEnumerable<MemberAPIApp.Program.Memo> membersFound)
{
}

Open in new window

kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
IMO, the reason why var is confusing is that var was originally created to support LINQ queries, where in your query you can declare a new, anonymous type:

e.g.

List<int> someInts = new List<int>() { 1, 1, 2, 3, 5, 8, 13 };
var fibonaccis = from value in someInts
                 select new
                 {
                     Value = value,
                     IsEven = (value % 2 == 0),
                     IsPowerOfThree = (value % 3 == 0),
                 };

foreach (var fibonacci in fibonaccis)
{
    Console.WriteLine($"Value {fibonacci.Value} is {(fibonacci.IsEven ? "even" : "odd")} and {(fibonacci.IsPowerOfThree ? "is" : "is not")} a power of three.");
}

Open in new window


In the above, the select creates a new, anonymous type which has three properties:  Value, IsEven, and IsPowerOfThree. I haven't declared a class or struct which defines this type...the compiler will do that. Since the compiler creates the type, I don't know what the name of the type is. However, if I use var, then I can let the compiler fill in the type for me (at compile time). And my code will just work.

Now, the reason people use var, in my experience, is because:

  • It's just three letter to type
  • You can change the type of the variable by simply adjusting the right side of the equals
  • They come from a Javascript environment...which just LOVES var to pieces
  • They claim that it forces you to name your variables better

The first and second points above are unarguable--they're just facts. The third point is me taking a jab at Javascipt developers, but I think it's true to some degree. The fourth point I take issue with:  If you learn to name your variables well as a matter of practice, then it doesn't matter if you use var of the actual type.

The biggest problem I have with people who (ab)use var is that it's easy to get carried away. As Chris Stanyon mentioned, you can easily use var to capture the result of a function call. But as a maintainer, I now have to hover over the variable or the function call in order to determine the type of the variable. This is not good for maintenance. I should be able to glance at a piece of code and, in short order, determine what that code does by the way it is written.
RadhaKrishnaKiJayaAuthor Commented:
Chris Stanyon,
Now how to retrieve the rows from membersFound in the method InsertIntoYourDB?

Ex. memberCode, age, name, memberType, balanceAmount etc.

Thank you!
Chris StanyonWebDevCommented:
In a similar way to how you have your original code:

private void InsertIntoYourDB(IEnumerable<MemberAPIApp.Program.Memo> membersFound)
{
    foreach (var member in membersFound)
    {
        // do something with member (add it to your DB etc).

    }
}

Open in new window

Like I said before, how you add it to your DB will depend very much on how you've coded your data layer. If you're using EF for example, you'll instantiate a DbContext, add each member to a DbSet and call SaveChange(). If you're using raw SQL, then you'll create a connection to your DB and run an INSERT query.

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
RadhaKrishnaKiJayaAuthor Commented:
Chris Stanyon, Thank  for your excellent help.  It worked like a charm.  Thank you again!

Hello kaufmed, Your post was useful, but for my current issue Chris Stanyon's resolution worked the best for me.  Thank you again.
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
ASP.NET

From novice to tech pro — start learning today.