0

Is this mapping correct? The list can have multiple rows

I am very curious to know if say the list contains 3 rows, and all 3 rows will be mapped? Or do we need a looping?

public List<Data> DataDetails { get; set; }

public static DataDetails ToDataConvert ( this Datadto data)
{

 DataDetails = dto.DataDetails.Select(x => new Data()
                        {
                            Name = x.Name,
                            id = x.Id
                        })
                        .ToList()
}
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 1
    Does it compile? That's often a very good sign. A LINQ `.Select` operation is a _projection_, it selects the columns of data you are interested in, but returns all rows. Somewhat confusingly, a LINQ `.Where` operation is a _selection_, picking out which rows should be in the result. See: https://stackoverflow.com/questions/1031076/what-are-projection-and-selection – Flydog57 Jan 04 '19 at 03:51
  • 3
    The return type of this method should be List instead of DataDetails. – Xiaosu Jan 04 '19 at 03:53
  • 1
    This code will not compile, and its wrong on several levels, we cant help you as this is not your real attempt or real code – TheGeneral Jan 04 '19 at 03:54
  • @Xiaosu: Thank you, I have the good code, but just wanna double check if what I am doing (MY intention is map list to list) is correct – Jasmine Jan 04 '19 at 04:12

2 Answers2

0

I don't know where is this came from dto (dto.DataDetails)

This dto.DataDetails Depends on how many rows you have created.

Your Code:

DataDetails = dto.DataDetails.Select(x => new Data
                        {
                            Name = x.Name,
                            id = x.Id
                        })
                        .ToList()

Your DataDetails Depends on this public List<Data> DataDetails { get; set; } the Data List

As a Result of your DataDetails Depend on this part of code:

     {
         Name = x.Name,
          id = x.Id
     })

If Your Data Class look like this

public String Name {get;set;}
public int id{get;set;}
public String Address {get;set;}

This means you did not include the Address of your Data Class as an example. As a Result you can get an Address but it returns a value of null.

Now if your linq looks like this:

dto.DataDetails.ToList()

this will include all the records you wanted to get but you need to use this class DataDetails as your variable something like:

VarFromDataDetailsClass =  dto.DataDetails.ToList();

or

var Result =  dto.DataDetails.ToList();
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
0

It seems that you want to use extension method in c# and convert a list. If so, I suggest you could use your converted list as the return value of your method. My extension method convert object of type List

namespace ServiceInterface.Models
{
public static  class ExtensionClass
{

    public static List<Customer> ConvertCustomer(this List<Customer> customers)
    {
        return customers.Select(
            c =>

            new Customer
            {
                FirstName = c.FirstName + " " + c.LastName,
                LastName = "",
                Gender = c.Gender
            }).ToList();
    }
}
}

Then you could write use the extension method as follows. Please don't forget to import the namespace of your extension method.

using ServiceInterface.Models;
namespace consoleClient.Message_exe
{
    class StringTest
    {
    static void Main(string[] args)
    {
             List<Customer> customers = new List<Customer>()
        {
            new Customer{LastName="a", FirstName="b"},
            new Customer{LastName="c", FirstName="d"}
        };

    }
}

The result.

enter image description here

Ackelry Xu
  • 756
  • 3
  • 6