Shakeer Hussain
Shakeer Hussain

Reputation: 2536

How to convert Generic.List values into extension method

I am using Linq to Entity , Linq query returning a ToList() and storing multiple rows in DataSourcesModel class. Later i am using foreach to iterate items and store values in DataSourcesModelDTO, till here working fine.

Now question is DataSourcesModelDTO dto storing only one row value. I am expecting it should store multiple row value. How to achieve this in extension method?

** var datasoruceModeltDTO = DataSourcesDTOTransformers.ToDTO(result);**

public static class DataSourcesDTOTransformers
    {
        public static DataSourcesModelDTO ToDTO(this List<DataSourcesModel> model)
        {
            if (model == null) { return null; }
            var dto = new DataSourcesModelDTO();
            ToDTO(model, dto);
            return dto;
        }
        public static void ToDTO(List<DataSourcesModel> model1, DataSourcesModelDTO dto)
        {
            foreach (var model in model1)
            {

                dto.DataSourceConfigID = model.DataSourceConfigID.ToString();
                dto.DataSourceID = model.DataSourceID.ToString();
                dto.Name = model.Name;
                dto.server = model.server;
                dto.LastModified = model.LastModified;
                dto.Instance = model.Instance;
            }


        }
    }



[DataContract]
    public class DataSourcesModelDTO
    {
        [DataMember]
        public string DataSourceID { get; set; }

        [DataMember]
        public string DataSourceConfigID { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string server { get; set; }

        [DataMember]
        public string Instance { get; set; }

        [DataMember]
        public bool Deleted { get; set; }

        [DataMember]
        public DateTime LastModified { get; set; }

        [DataMember]
        public DateTime LastSync { get; set; }

    }

Upvotes: 0

Views: 303

Answers (1)

D Stanley
D Stanley

Reputation: 152596

foreach (var model in model1)
{

    dto.DataSourceConfigID = model.DataSourceConfigID.ToString();

You are setting the properties of the same dto object every time in the loop. I would rearrange your toDTO methiod to either:

  • take in a single DataSourcesModel object and return a single DataSourcesModelDTO object (calling the method from within a loop or Linq query), or
  • take in an IEnumerable<DataSourcesModel> and return an IEnumerable<DataSourcesModelDTO>

How to achieve this in extension method?

Not sure why you need an extension method, but the appropriate signature would be:

public static IEnumerable<DataSourcesModelDTO> ToDTOCollection (this IEnumerable<DataSourcesModel> model)

I'll let you work out what the implementation would be. Hint: You could create a List<DataSourcesModelDTO> within the method and populate it with new objects the same way you are now.

Upvotes: 1

Related Questions