Using LINQ to convert List to List












40















I have 2 classes which have some identical properties.
I stock into a list properties from 1st class, and after that, i want to take some needed properties and put them into a list of 2nd class type.
I've made cast sequence through C# and that runs OK, but i must do with LINQ. I tried to do something but without good results. Help me please with suggestions.



1st Class:



   public class ServiceInfo {
private long _id;
public long ID {
get { return this._id; }
set { _id = value; }
}

private string _name;
public string Name {
get { return this._name; }
set { _name = value; }
}

private long _qty;
public long Quantity {
get { return this._qty; }
set { _qty = value; }
}

private double _amount;
public double Amount {
get { return this._amount; }
set { _amount = value; }
}

private string _currency;
public string Currency {
get { return this._currency; }
set { _currency = value; }
}

private DateTime? _date;
public DateTime? Date {
get { return this._date; }
set { _date = value; }
}
}


2nd Class:



class InvoiceWithEntryInfo {
private string currencyField;

private long IdField;
public long IdIWEI {
get { return this.IdField; }
set { IdIWEI = value; }
}

private string nameField;
public string NameIWEI {
get { return this.nameField; }
set { NameIWEI = value; }
}

private long qtyField;
public long QuantityIWEI {
get { return this.qtyField; }
set { QuantityIWEI = value; }
}

private double amountField;
public double AmountIWEI {
get { return this.amountField; }
set { AmountIWEI = value; }
}

private DateTime dateField;
public DateTime? DateIWEI {
get { return this.dateField; }
set { DateIWEI = value; }
}

public string OwnerIWEI {
get; set;
}
}


C# sample which runs OK:
...



var sil = new List<ServiceInfo>();
var iweil = new List<InvoiceWithEntryInfo>();


...



if (sil != null)
{
foreach (ServiceInfo item in sil)
{
iweil.Add(new InvoiceWithEntryInfo
{
IdIWEI = item.ID,
AmountIWEI = item.Amount,
DateIWEI = item.Date
});
}


LINQ sample wich doesnt run OK:



            iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);









share|improve this question





























    40















    I have 2 classes which have some identical properties.
    I stock into a list properties from 1st class, and after that, i want to take some needed properties and put them into a list of 2nd class type.
    I've made cast sequence through C# and that runs OK, but i must do with LINQ. I tried to do something but without good results. Help me please with suggestions.



    1st Class:



       public class ServiceInfo {
    private long _id;
    public long ID {
    get { return this._id; }
    set { _id = value; }
    }

    private string _name;
    public string Name {
    get { return this._name; }
    set { _name = value; }
    }

    private long _qty;
    public long Quantity {
    get { return this._qty; }
    set { _qty = value; }
    }

    private double _amount;
    public double Amount {
    get { return this._amount; }
    set { _amount = value; }
    }

    private string _currency;
    public string Currency {
    get { return this._currency; }
    set { _currency = value; }
    }

    private DateTime? _date;
    public DateTime? Date {
    get { return this._date; }
    set { _date = value; }
    }
    }


    2nd Class:



    class InvoiceWithEntryInfo {
    private string currencyField;

    private long IdField;
    public long IdIWEI {
    get { return this.IdField; }
    set { IdIWEI = value; }
    }

    private string nameField;
    public string NameIWEI {
    get { return this.nameField; }
    set { NameIWEI = value; }
    }

    private long qtyField;
    public long QuantityIWEI {
    get { return this.qtyField; }
    set { QuantityIWEI = value; }
    }

    private double amountField;
    public double AmountIWEI {
    get { return this.amountField; }
    set { AmountIWEI = value; }
    }

    private DateTime dateField;
    public DateTime? DateIWEI {
    get { return this.dateField; }
    set { DateIWEI = value; }
    }

    public string OwnerIWEI {
    get; set;
    }
    }


    C# sample which runs OK:
    ...



    var sil = new List<ServiceInfo>();
    var iweil = new List<InvoiceWithEntryInfo>();


    ...



    if (sil != null)
    {
    foreach (ServiceInfo item in sil)
    {
    iweil.Add(new InvoiceWithEntryInfo
    {
    IdIWEI = item.ID,
    AmountIWEI = item.Amount,
    DateIWEI = item.Date
    });
    }


    LINQ sample wich doesnt run OK:



                iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

    iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);









    share|improve this question



























      40












      40








      40


      7






      I have 2 classes which have some identical properties.
      I stock into a list properties from 1st class, and after that, i want to take some needed properties and put them into a list of 2nd class type.
      I've made cast sequence through C# and that runs OK, but i must do with LINQ. I tried to do something but without good results. Help me please with suggestions.



      1st Class:



         public class ServiceInfo {
      private long _id;
      public long ID {
      get { return this._id; }
      set { _id = value; }
      }

      private string _name;
      public string Name {
      get { return this._name; }
      set { _name = value; }
      }

      private long _qty;
      public long Quantity {
      get { return this._qty; }
      set { _qty = value; }
      }

      private double _amount;
      public double Amount {
      get { return this._amount; }
      set { _amount = value; }
      }

      private string _currency;
      public string Currency {
      get { return this._currency; }
      set { _currency = value; }
      }

      private DateTime? _date;
      public DateTime? Date {
      get { return this._date; }
      set { _date = value; }
      }
      }


      2nd Class:



      class InvoiceWithEntryInfo {
      private string currencyField;

      private long IdField;
      public long IdIWEI {
      get { return this.IdField; }
      set { IdIWEI = value; }
      }

      private string nameField;
      public string NameIWEI {
      get { return this.nameField; }
      set { NameIWEI = value; }
      }

      private long qtyField;
      public long QuantityIWEI {
      get { return this.qtyField; }
      set { QuantityIWEI = value; }
      }

      private double amountField;
      public double AmountIWEI {
      get { return this.amountField; }
      set { AmountIWEI = value; }
      }

      private DateTime dateField;
      public DateTime? DateIWEI {
      get { return this.dateField; }
      set { DateIWEI = value; }
      }

      public string OwnerIWEI {
      get; set;
      }
      }


      C# sample which runs OK:
      ...



      var sil = new List<ServiceInfo>();
      var iweil = new List<InvoiceWithEntryInfo>();


      ...



      if (sil != null)
      {
      foreach (ServiceInfo item in sil)
      {
      iweil.Add(new InvoiceWithEntryInfo
      {
      IdIWEI = item.ID,
      AmountIWEI = item.Amount,
      DateIWEI = item.Date
      });
      }


      LINQ sample wich doesnt run OK:



                  iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

      iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);









      share|improve this question
















      I have 2 classes which have some identical properties.
      I stock into a list properties from 1st class, and after that, i want to take some needed properties and put them into a list of 2nd class type.
      I've made cast sequence through C# and that runs OK, but i must do with LINQ. I tried to do something but without good results. Help me please with suggestions.



      1st Class:



         public class ServiceInfo {
      private long _id;
      public long ID {
      get { return this._id; }
      set { _id = value; }
      }

      private string _name;
      public string Name {
      get { return this._name; }
      set { _name = value; }
      }

      private long _qty;
      public long Quantity {
      get { return this._qty; }
      set { _qty = value; }
      }

      private double _amount;
      public double Amount {
      get { return this._amount; }
      set { _amount = value; }
      }

      private string _currency;
      public string Currency {
      get { return this._currency; }
      set { _currency = value; }
      }

      private DateTime? _date;
      public DateTime? Date {
      get { return this._date; }
      set { _date = value; }
      }
      }


      2nd Class:



      class InvoiceWithEntryInfo {
      private string currencyField;

      private long IdField;
      public long IdIWEI {
      get { return this.IdField; }
      set { IdIWEI = value; }
      }

      private string nameField;
      public string NameIWEI {
      get { return this.nameField; }
      set { NameIWEI = value; }
      }

      private long qtyField;
      public long QuantityIWEI {
      get { return this.qtyField; }
      set { QuantityIWEI = value; }
      }

      private double amountField;
      public double AmountIWEI {
      get { return this.amountField; }
      set { AmountIWEI = value; }
      }

      private DateTime dateField;
      public DateTime? DateIWEI {
      get { return this.dateField; }
      set { DateIWEI = value; }
      }

      public string OwnerIWEI {
      get; set;
      }
      }


      C# sample which runs OK:
      ...



      var sil = new List<ServiceInfo>();
      var iweil = new List<InvoiceWithEntryInfo>();


      ...



      if (sil != null)
      {
      foreach (ServiceInfo item in sil)
      {
      iweil.Add(new InvoiceWithEntryInfo
      {
      IdIWEI = item.ID,
      AmountIWEI = item.Amount,
      DateIWEI = item.Date
      });
      }


      LINQ sample wich doesnt run OK:



                  iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

      iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);






      c# linq list .net-3.5 casting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 '14 at 5:54







      mihai

















      asked Aug 16 '11 at 8:38









      mihaimihai

      1,46222246




      1,46222246
























          5 Answers
          5






          active

          oldest

          votes


















          69














          var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
          {
          IdWEI = item.Id,
          NameWEI = item.Name,
          ....
          }).ToList();





          share|improve this answer

































            11














              var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
            IdIWEI = item.ID,
            AmountIWEI = item.Amount,
            DateIWEI = item.Date}).ToList();





            share|improve this answer

































              9














              You need a function to convert a T instance to a U instance:



              ResultType ConvertMethod(StartType input)


              and you need to write this. Then



              outputList = inputList.Select(ConvertMethod).ToList();


              will apply it to the whole input collection. The conversion function can be a lambda written inline but doesn't need to be (if the function has the right signature, like ConvertMethod then the compiler will convert it correctly to pass to Select).






              share|improve this answer



















              • 1





                Could you provide some real example, pls? Your answer is the best one!

                – Academy of Programmer
                Jan 10 '14 at 12:32











              • List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                – Win Man
                May 5 '17 at 23:19





















              5














              Just use Select:



              if(sil != null)
              {
              var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
              {
              IdIWEI = item.ID,
              AmountIWEI = item.Amount,
              DateIWEI = item.Date
              }).ToList();
              }





              share|improve this answer































                2














                Your regular C# code and LINQ are not equivalent. In the regular C# you instantiate a new instance of the other class and initialize the properties, whereas you try to cast (well convert) from one to the other; however, since they are not in the same class hierarchy you can't cast, and as you haven't defined a conversion operator, you can't convert (using cast syntax) either.



                You either have to define a conversion operator



                public static explicit operator InvoiceWithEntryInfo(ServiceInfo item){
                return new InvoiceWithEntryInfo {
                IdIWEI = item.ID,
                AmountIWEI = item.Amount,
                DateIWEI = item.Date};
                }


                or a creation method using regular method signature. I'd suggest the latter since the former pretend to be something it's not. It's not a cast and I'd personally like to be able to see that the code creates a new instance based on some input.






                share|improve this answer

























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7075572%2fusing-linq-to-convert-listu-to-listt%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  69














                  var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
                  {
                  IdWEI = item.Id,
                  NameWEI = item.Name,
                  ....
                  }).ToList();





                  share|improve this answer






























                    69














                    var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
                    {
                    IdWEI = item.Id,
                    NameWEI = item.Name,
                    ....
                    }).ToList();





                    share|improve this answer




























                      69












                      69








                      69







                      var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
                      {
                      IdWEI = item.Id,
                      NameWEI = item.Name,
                      ....
                      }).ToList();





                      share|improve this answer















                      var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
                      {
                      IdWEI = item.Id,
                      NameWEI = item.Name,
                      ....
                      }).ToList();






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 10 '13 at 7:07









                      edosoft

                      11.4k2371106




                      11.4k2371106










                      answered Aug 16 '11 at 8:45









                      vc 74vc 74

                      28.2k64875




                      28.2k64875

























                          11














                            var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
                          IdIWEI = item.ID,
                          AmountIWEI = item.Amount,
                          DateIWEI = item.Date}).ToList();





                          share|improve this answer






























                            11














                              var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
                            IdIWEI = item.ID,
                            AmountIWEI = item.Amount,
                            DateIWEI = item.Date}).ToList();





                            share|improve this answer




























                              11












                              11








                              11







                                var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
                              IdIWEI = item.ID,
                              AmountIWEI = item.Amount,
                              DateIWEI = item.Date}).ToList();





                              share|improve this answer















                                var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
                              IdIWEI = item.ID,
                              AmountIWEI = item.Amount,
                              DateIWEI = item.Date}).ToList();






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Sep 11 '17 at 14:12









                              Diogo Cid

                              3,23311219




                              3,23311219










                              answered Aug 16 '11 at 8:43









                              Bob ValeBob Vale

                              15.8k2742




                              15.8k2742























                                  9














                                  You need a function to convert a T instance to a U instance:



                                  ResultType ConvertMethod(StartType input)


                                  and you need to write this. Then



                                  outputList = inputList.Select(ConvertMethod).ToList();


                                  will apply it to the whole input collection. The conversion function can be a lambda written inline but doesn't need to be (if the function has the right signature, like ConvertMethod then the compiler will convert it correctly to pass to Select).






                                  share|improve this answer



















                                  • 1





                                    Could you provide some real example, pls? Your answer is the best one!

                                    – Academy of Programmer
                                    Jan 10 '14 at 12:32











                                  • List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                    – Win Man
                                    May 5 '17 at 23:19


















                                  9














                                  You need a function to convert a T instance to a U instance:



                                  ResultType ConvertMethod(StartType input)


                                  and you need to write this. Then



                                  outputList = inputList.Select(ConvertMethod).ToList();


                                  will apply it to the whole input collection. The conversion function can be a lambda written inline but doesn't need to be (if the function has the right signature, like ConvertMethod then the compiler will convert it correctly to pass to Select).






                                  share|improve this answer



















                                  • 1





                                    Could you provide some real example, pls? Your answer is the best one!

                                    – Academy of Programmer
                                    Jan 10 '14 at 12:32











                                  • List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                    – Win Man
                                    May 5 '17 at 23:19
















                                  9












                                  9








                                  9







                                  You need a function to convert a T instance to a U instance:



                                  ResultType ConvertMethod(StartType input)


                                  and you need to write this. Then



                                  outputList = inputList.Select(ConvertMethod).ToList();


                                  will apply it to the whole input collection. The conversion function can be a lambda written inline but doesn't need to be (if the function has the right signature, like ConvertMethod then the compiler will convert it correctly to pass to Select).






                                  share|improve this answer













                                  You need a function to convert a T instance to a U instance:



                                  ResultType ConvertMethod(StartType input)


                                  and you need to write this. Then



                                  outputList = inputList.Select(ConvertMethod).ToList();


                                  will apply it to the whole input collection. The conversion function can be a lambda written inline but doesn't need to be (if the function has the right signature, like ConvertMethod then the compiler will convert it correctly to pass to Select).







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 16 '11 at 8:45









                                  RichardRichard

                                  89.1k17152220




                                  89.1k17152220








                                  • 1





                                    Could you provide some real example, pls? Your answer is the best one!

                                    – Academy of Programmer
                                    Jan 10 '14 at 12:32











                                  • List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                    – Win Man
                                    May 5 '17 at 23:19
















                                  • 1





                                    Could you provide some real example, pls? Your answer is the best one!

                                    – Academy of Programmer
                                    Jan 10 '14 at 12:32











                                  • List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                    – Win Man
                                    May 5 '17 at 23:19










                                  1




                                  1





                                  Could you provide some real example, pls? Your answer is the best one!

                                  – Academy of Programmer
                                  Jan 10 '14 at 12:32





                                  Could you provide some real example, pls? Your answer is the best one!

                                  – Academy of Programmer
                                  Jan 10 '14 at 12:32













                                  List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                  – Win Man
                                  May 5 '17 at 23:19







                                  List<int> numbers = new List<int>() { 1, 2, 3 }; Func<int, string> numToStringWithLPad = i => "Number: " + i; var numAsString = numbers.Select(numToStringWithLPad); numAsString.ToList().ForEach(i => Console.WriteLine(i)); Output: Number: 1 Number: 2 Number: 3

                                  – Win Man
                                  May 5 '17 at 23:19













                                  5














                                  Just use Select:



                                  if(sil != null)
                                  {
                                  var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
                                  {
                                  IdIWEI = item.ID,
                                  AmountIWEI = item.Amount,
                                  DateIWEI = item.Date
                                  }).ToList();
                                  }





                                  share|improve this answer




























                                    5














                                    Just use Select:



                                    if(sil != null)
                                    {
                                    var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
                                    {
                                    IdIWEI = item.ID,
                                    AmountIWEI = item.Amount,
                                    DateIWEI = item.Date
                                    }).ToList();
                                    }





                                    share|improve this answer


























                                      5












                                      5








                                      5







                                      Just use Select:



                                      if(sil != null)
                                      {
                                      var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
                                      {
                                      IdIWEI = item.ID,
                                      AmountIWEI = item.Amount,
                                      DateIWEI = item.Date
                                      }).ToList();
                                      }





                                      share|improve this answer













                                      Just use Select:



                                      if(sil != null)
                                      {
                                      var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
                                      {
                                      IdIWEI = item.ID,
                                      AmountIWEI = item.Amount,
                                      DateIWEI = item.Date
                                      }).ToList();
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 16 '11 at 8:43









                                      ForbesLindesayForbesLindesay

                                      7,39623367




                                      7,39623367























                                          2














                                          Your regular C# code and LINQ are not equivalent. In the regular C# you instantiate a new instance of the other class and initialize the properties, whereas you try to cast (well convert) from one to the other; however, since they are not in the same class hierarchy you can't cast, and as you haven't defined a conversion operator, you can't convert (using cast syntax) either.



                                          You either have to define a conversion operator



                                          public static explicit operator InvoiceWithEntryInfo(ServiceInfo item){
                                          return new InvoiceWithEntryInfo {
                                          IdIWEI = item.ID,
                                          AmountIWEI = item.Amount,
                                          DateIWEI = item.Date};
                                          }


                                          or a creation method using regular method signature. I'd suggest the latter since the former pretend to be something it's not. It's not a cast and I'd personally like to be able to see that the code creates a new instance based on some input.






                                          share|improve this answer






























                                            2














                                            Your regular C# code and LINQ are not equivalent. In the regular C# you instantiate a new instance of the other class and initialize the properties, whereas you try to cast (well convert) from one to the other; however, since they are not in the same class hierarchy you can't cast, and as you haven't defined a conversion operator, you can't convert (using cast syntax) either.



                                            You either have to define a conversion operator



                                            public static explicit operator InvoiceWithEntryInfo(ServiceInfo item){
                                            return new InvoiceWithEntryInfo {
                                            IdIWEI = item.ID,
                                            AmountIWEI = item.Amount,
                                            DateIWEI = item.Date};
                                            }


                                            or a creation method using regular method signature. I'd suggest the latter since the former pretend to be something it's not. It's not a cast and I'd personally like to be able to see that the code creates a new instance based on some input.






                                            share|improve this answer




























                                              2












                                              2








                                              2







                                              Your regular C# code and LINQ are not equivalent. In the regular C# you instantiate a new instance of the other class and initialize the properties, whereas you try to cast (well convert) from one to the other; however, since they are not in the same class hierarchy you can't cast, and as you haven't defined a conversion operator, you can't convert (using cast syntax) either.



                                              You either have to define a conversion operator



                                              public static explicit operator InvoiceWithEntryInfo(ServiceInfo item){
                                              return new InvoiceWithEntryInfo {
                                              IdIWEI = item.ID,
                                              AmountIWEI = item.Amount,
                                              DateIWEI = item.Date};
                                              }


                                              or a creation method using regular method signature. I'd suggest the latter since the former pretend to be something it's not. It's not a cast and I'd personally like to be able to see that the code creates a new instance based on some input.






                                              share|improve this answer















                                              Your regular C# code and LINQ are not equivalent. In the regular C# you instantiate a new instance of the other class and initialize the properties, whereas you try to cast (well convert) from one to the other; however, since they are not in the same class hierarchy you can't cast, and as you haven't defined a conversion operator, you can't convert (using cast syntax) either.



                                              You either have to define a conversion operator



                                              public static explicit operator InvoiceWithEntryInfo(ServiceInfo item){
                                              return new InvoiceWithEntryInfo {
                                              IdIWEI = item.ID,
                                              AmountIWEI = item.Amount,
                                              DateIWEI = item.Date};
                                              }


                                              or a creation method using regular method signature. I'd suggest the latter since the former pretend to be something it's not. It's not a cast and I'd personally like to be able to see that the code creates a new instance based on some input.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Aug 16 '11 at 11:56









                                              Clint

                                              4,70511837




                                              4,70511837










                                              answered Aug 16 '11 at 9:00









                                              Rune FSRune FS

                                              18.3k64887




                                              18.3k64887






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7075572%2fusing-linq-to-convert-listu-to-listt%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                                  SQL update select statement

                                                  'app-layout' is not a known element: how to share Component with different Modules