1

I have this code here:

for (int i = 0; i < reader.FieldCount; i++)
{
    RedBlue item = new RedBlue();

    if (reader.GetName(i).ToString().Contains("BID"))
    {
        item.baselinefinish = reader.GetValue(i).ToString();
    }
    if (reader.GetName(i).ToString().Contains("AID"))
    {
        item.actualenddate = reader.GetValue(i).ToString();
    }

    redBlue.Add(item);
}

What I am trying to do loop through data and add it to a class, but my problem is in my class I have two strings and I want to populate each string like this (first string gets the first item in the loop, the second string get the second item in the loop, and keep going like that, so instead of each one in the loop, for every two items in the loop add them to the string and continue on....I really hope this makes sense. Anyone know how I would accomplish this?

Currently what is happening, is it will add one of the strings to the class and then add the second string to a new class.

4
  • So you want to have some automatic mapping of fields between the data you are reading with reader and the data in your new RedBlue object, right ? Commented Jan 8, 2018 at 17:18
  • I guess so, I've been banging my head against a wall to figure this out Commented Jan 8, 2018 at 17:19
  • To do that somehow automatically, you should have a look at Automapper automapper.readthedocs.io/en/latest/Getting-started.html Commented Jan 8, 2018 at 17:21
  • "I really hope this makes sense" not entirely. It does sound like you are trying to read Properties of an (database?) entity and add them (dynamically) to your class RedBlue? Please show us the definition for both (db entity) and RedBlue. Additionally what does this line "redBlue.Add(item); what is "redBlue" is it an instance of RedBlue ? Commented Jan 8, 2018 at 17:28

3 Answers 3

1

You can use Automapper and do something like this :

(adapted from what I remember of this framework, the docs here and your example)

// Configure AutoMapper
Mapper.Initialize(cfg =>
  cfg.CreateMap<YourReaderClass, RedBlue>()
    .ForMember(dest => dest.baselinefinish , opt => opt.MapFrom(src => src.BID))
    .ForMember(dest => dest.actualenddate , opt => opt.MapFrom(src => src.AID))

// Perform mapping
RedBlue item = Mapper.Map<YourReaderClass, RedBlue>(reader);

You do the configuration once somewhere and then you can perform as many mapping you want. Of course, you have to manually indicate which field is mapped to which field with as many ForMember as you need.


EDIT

Actually, you could of course still do it without 3rd party, as you were thinking. To solve the specific problem with your method :

Currently what is happening, is it will add one of the strings to the class and then add the second string to a new class.

(by the way, you mean instance of your class (object), not class )

Of course this happening, because you are creating new objects each time you iterate your loop !

If you do it like this, it should work :

// instantiate your object once, before the loop :
RedBlue item = new RedBlue();

for (int i = 0; i < reader.FieldCount; i++)
{
    if (reader.GetName(i).ToString().Contains("BID"))
    {
        item.baselinefinish = reader.GetValue(i).ToString();
    }
    if (reader.GetName(i).ToString().Contains("AID"))
    {
        item.actualenddate = reader.GetValue(i).ToString();
    }
}

// now you have one object named 'item' which should be what you want.
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think solves the problem if there are more than 2 fields. It will start overwriting the data. I imagine that data is something like BID Value,AID Value,BID Value,AID Value,BID Value,AID Value...etc
0

Not sure I follow, but right now you are creating a new object at every iteration and therefore each object will have only either string.

If your data is as you say, containing BID every second and AID every second element, as a poor mans solution, you could simply increment the i after adding the first string.

for (int i = 0; i < reader.FieldCount; i++)
{
    RedBlue item = new RedBlue();

    if (reader.GetName(i).ToString().Contains("BID"))
    {
        item.baselinefinish = reader.GetValue(i).ToString();
        i++;
    }
    if (reader.GetName(i).ToString().Contains("AID"))
    {
        item.actualenddate = reader.GetValue(i).ToString();
    }

    redBlue.Add(item);
}

Or maybe I'm missing something?

Comments

0

Not sure if I follow your idea, but you can use even or odd index number to represent the first and second item on the loop.

Something like that:

for (int i = 0; i < reader.FieldCount; i++)
{
    RedBlue item = null;
    //if it's even index number
    if(i % 2 == 0){
      item = new RedBlue();
      redBlue.Add(item);

      if (reader.GetName(i).ToString().Contains("BID"))
         item.baselinefinish = reader.GetValue(i).ToString();

    }else{

      if (reader.GetName(i).ToString().Contains("AID"))
        item.actualenddate = reader.GetValue(i).ToString();
    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.