我有2個類有一些相同的屬性。 我從第一類股票進入列表屬性,之後,我想採取一些需要的屬性,並把它們放入第二類類型的列表。 我已經通過C#製作了序列,並且運行正常,但是我必須使用LINQ。我試圖做一些事情,但沒有好的結果。請提出建議幫助我。使用LINQ轉換列表<U>到列表<T>
第一類:
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#示例它運行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樣品至極犯規運行OK:
iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);
iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);
:)我在2011年開始。現在,當我看到這個問題時,我笑了起來。 (輕笑)美好的時光! – meorfi