2011-08-16 107 views
33

我有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); 
+3

:)我在2011年開始。現在,當我看到這個問題時,我笑了起來。 (輕笑)美好的時光! – meorfi

回答

59
var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo() 
{ 
    IdWEI = item.Id, 
    NameWEI = item.Name, 
    .... 
}).ToList(); 
10
var iweil = sil.Select(item=> new InvoiceWithEntryInfo { 
       IdIWEI = item.ID, 
       AmountIWEI = item.Amount, 
       DateIWEI = item.Date}).ToList(); 
5

只需使用選擇:

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

你需要一個函數的T實例轉換爲U實例:

ResultType ConvertMethod(StartType input) 

你需要寫這個。然後

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

將其應用於整個輸入集合。轉換函數可以是內聯寫入的lambda表達式,但不需要(如果函數具有正確的簽名,如ConvertMethod那麼編譯器將正確地將其轉換爲傳遞給Select)。

+1

你能提供一些真實的例子嗎?你的答案是最好的! –

+0

List numbers = new List (){1,2,3}; Func numToStringWithLPad = i =>「Number:」+ i; var numAsString =數字。選擇(numToStringWithLPad); numAsString.ToList()。ForEach(i => Console.WriteLine(i)); 輸出: 編號:1 編號:2 編號:3 –

2

您的常規C#代碼和LINQ並不等同。在常規的C#中,你實例化另一個類的新實例並初始化屬性,而你試圖從一個到另一個進行轉換(轉換);但是,由於它們不在相同的類層次結構中,因此不能進行強制轉換,並且由於您尚未定義轉換運算符,因此無法轉換(使用強制轉換語法)。

要麼你必須定義一個轉換操作符

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

或使用常規的方法簽名創建方法。我會建議後者假裝自己不是。這不是演員陣容,我個人希望能夠看到代碼基於某些輸入創建了一個新實例。

相關問題