Parsing API Call data

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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

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

Author

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.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

ǩ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:
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.

Author

Commented:
Chris Stanyon,
Now how to retrieve the rows from membersFound in the method InsertIntoYourDB?

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

Thank you!
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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.

Author

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.

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