ckv
ckv

Reputation: 10830

Convert one set of objects to another set of objects of different class in single line. C#

I have 2 classes with different properties in each. Also I have a collection of one set of objects of the class A. Now I want to copy these to an array of objects of Class B.

The 2 classes are not inter related and also the fields are different in each. SO i have to explicitly map the fields i want to copy. Right now I am using a foreach to copy individual element. Is there a shorter way to accomplish this. This is the class B

 public class Event
        {
            public string EventOriginTime { get; set; }
            public string EventReceivedTime { get; set; }
            public int EventCode { get; set; }
            public string CardNumber { get; set; }
            public string ReaderName { get; set; }

        }

First class A also will appear something like this but that is a 3rd party class.

Current solution I have is:

List<Event> listOfEvents = new List<Event>();
            foreach (var eachEvent in eventsFromArgus)
            {
                listOfEvents.Add( new Event
                { 
                    ReaderName = eachEvent.DeviceName, 
                    EventCode = eachEvent.EventCode, 
                    EventReceivedTime = eachEvent.ReceiveTime.ToString(), 
                    EventOriginTime = eachEvent.OriginTime.ToString(), 
                    CardNumber = eachEvent.CredentialIdentifier
                });
            }

Upvotes: 3

Views: 102

Answers (4)

Jason Down
Jason Down

Reputation: 22171

Another approach is to pass the third party object directly into the Event class' constructor:

public class Event
{
    private readonly ThirdPartyClass _eventFromArgus;

    public Event(ThirdPartyClass eventFromArgus)
    {
        _eventFromArgus = eventFromArgus;
    }

    public string ReaderName { get { return _eventFromArgus.DeviceName; } }
    // etc.
}

Then you can just do this:

var listOfEvents = eventsFromArgus.Select(eachEvent => new Event(eachEvent));

Upvotes: 3

ptutt
ptutt

Reputation: 1338

You could add a constructor to the Event class:

public Event(string EventOriginTime, string EventReceivedTime, int EventCode, string CardNumber, string ReaderName)
{
    this.EventOriginTime = EventOriginTime;
    this.EventReceivedTime = EventReceivedTime;
    this.EventCode = EventCode;
    this.CardNumber = CardNumber;
    this.ReaderName = ReaderName;
}

Then at least you don't have to specify the field names when creating a new instance.

List<Event> listOfEvents = new List<Event>();
foreach (var eachEvent in eventsFromArgus)
{
    listOfEvents.Add(new Event(eachEvent.OriginTime.ToString(), eachEvent.ReceiveTime.ToString(), eachEvent.EventCode, eachEvent.DeviceName)
}

Upvotes: 1

cnd
cnd

Reputation: 33754

also with linq as Jim suggested but a bit different

var listOfEvents = eventsFromArgus.Select(eachEvent => 
    new Event( ReaderName = eachEvent.DeviceName, 
               EventCode = eachEvent.EventCode, 
               EventReceivedTime = eachEvent.ReceiveTime.ToString(), 
               EventOriginTime = eachEvent.OriginTime.ToString(), 
               CardNumber = eachEvent.CredentialIdentifier)).ToList();

Upvotes: 2

Jim Mischel
Jim Mischel

Reputation: 133995

You could use LINQ:

List<event> listOfEvents = 
    (from eachEvent in eventsFromArgus
     select new Event(
         ReaderName = eachEvent.DeviceName, 
         EventCode = eachEvent.EventCode, 
         EventReceivedTime = eachEvent.ReceiveTime.ToString(), 
         EventOriginTime = eachEvent.OriginTime.ToString(), 
         CardNumber = eachEvent.CredentialIdentifier)).ToList();

But that's not terribly different from what you already have.

Or, you could look into something like AutoMapper.

Upvotes: 4

Related Questions