LINQ将List <U>转换为List <T>

我有2个类,它们具有一些相同的属性。我将第一个类的属性存储到列表中,然后,我想获取一些需要的属性并将它们放入第二类类型的列表中。我已经通过C#进行了强制转换,并且运行正常,但是我必须使用LINQ。 我试图做点事情,但没有取得好的结果。 请给我一些建议。

一等舱:

   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; }
    }
}

二等舱:

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#示例:...

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
                });
        }

不能运行OK的LINQ示例:

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

            iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);
mihai asked 2020-07-04T19:15:13Z
5个解决方案
75 votes
var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
{
  IdWEI = item.Id,
  NameWEI = item.Name,
  ....
}).ToList();
vc 74 answered 2020-07-04T19:15:22Z
11 votes
  var iweil = sil.Select(item=> new InvoiceWithEntryInfo {
                 IdIWEI = item.ID,
                 AmountIWEI = item.Amount,
                 DateIWEI = item.Date}).ToList();
Bob Vale answered 2020-07-04T19:15:38Z
10 votes

您需要一个函数将ConvertMethod实例转换为Select实例:

ResultType ConvertMethod(StartType input)

你需要写这个 然后

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

会将其应用于整个输入集合。 转换函数可以是内联编写的lambda,但不必是(如果函数具有正确的签名,如ConvertMethod,则编译器会将其正确转换为传递给Select)。

Richard answered 2020-07-04T19:16:07Z
5 votes

只需使用Select:

if(sil != null)
{
   var iweil = sil.Select(item=>new InvoiceWithEntryInfo()
            {
                IdIWEI = item.ID,
                AmountIWEI = item.Amount,
                DateIWEI = item.Date
            }).ToList();
}
ForbesLindesay answered 2020-07-04T19:16:26Z
2 votes

您的常规C#代码和LINQ不等效。 在常规C#中,您实例化了另一个类的新实例并初始化属性,而您尝试将一个类转换为另一个(很好地进行转换)。 但是,由于它们不在同一类层次结构中,因此您无法进行转换,并且由于尚未定义转换运算符,因此也不能进行转换(使用转换语法)。

您要么必须定义一个转换运算符

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

或使用常规方法签名的创建方法。 我建议使用后者,因为前者假装不是。 这不是强制转换,我个人希望能够看到代码基于一些输入来创建新实例。

Rune FS answered 2020-07-04T19:16:56Z
translate from https://stackoverflow.com:/questions/7075572/using-linq-to-convert-listu-to-listt