2011-07-21 155 views
1

我想使用LINQ將數據從一個自定義集合傳遞到另一個集合。它的複雜性是因爲集合有2個子集合。使用Linq將數據從一個集合傳遞到另一個集合

希望將數據複製到:

public class Quote 
{ 
    public int Id { get; set; } 
    public string Type { get; set; } 
    public virtual ICollection<Rate> Rates { get; set; } 
} 

public class Rate 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Option> Options { get; set; } 
} 

public class Option 
{ 
    public int Id { get; set; } 
    public decimal Price { get; set; } 
} 

來自:

public class Quote 
{ 
    public int QuoteId { get; set; } 
    public string Type { get; set; } 
    public string Destination { get; set; } 
    public List<RateSet> RateSets { get; set; } 
} 


public class RateSet 
{ 
    public int Id { get; set; } 
    public decimal ValueMin { get; set; } 
    public decimal ValueMax { get; set; } 
    public List<Option> Options { get; set; } 
} 

public class Option 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public decimal? Price { get; set; } 
} 

我與此找到感覺,但保持打擊的問題...

newQuotes = Quotes 
    .Select(x => new Quote() { 
     Id = x.QuoteId, 
     Rates = x.RateSets.Select(y => new Rate() { 
      Id = y.Id, 
      Options = y.Options.Select(z => new Option() { 
       Id = z.Id, 
       Price = z.Price 
      }).ToList(),.... 

+2

「保持打擊的問題......」 不要告訴。 –

+0

你有什麼問題? –

回答

2

編譯沒有任何錯誤

// to 
public class Quote2 
{ 
    public int Id { get; set; } 
    public string Type { get; set; } 
    public virtual ICollection<Rate> Rates { get; set; } 
} 

public class Rate 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Option2> Options { get; set; } 
} 

public class Option2 
{ 
    public int Id { get; set; } 
    public decimal Price { get; set; } 
} 

// from 
public class Quote1 
{ 
    public int QuoteId { get; set; } 
    public string Type { get; set; } 
    public string Destination { get; set; } 
    public List<RateSet> RateSets { get; set; } 
} 


public class RateSet 
{ 
    public int Id { get; set; } 
    public decimal ValueMin { get; set; } 
    public decimal ValueMax { get; set; } 
    public List<Option1> Options { get; set; } 
} 

public class Option1 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public decimal? Price { get; set; } 
} 

void Main() 
{ 
    var Quotes = new List<Quote1>(); 

    var newQuotes = Quotes 
    .Select(x => new Quote2 { 
     Id = x.QuoteId, 
     Rates = x.RateSets == null ? null : x.RateSets.Select(y => new Rate { 
      Id = y.Id, 
      Options = y.Options == null ? null : y.Options.Select(z => new Option2 { 
       Id = z.Id, 
       Price = z.Price.Value 
      }).ToList()}).ToList()}).ToList(); 
} 
+0

這對我有用,一條LINQ語句就是我努力的方向。 – FloatLeft

+0

@FloatLeft:你應該重新思考這個。這一個linq語句難以維護和閱讀。 –

0

更簡單的方法可能會在每個類中創建將自身轉換爲其他類型的方法。或者,如果您不想要這種耦合,請創建一個工廠類,一次爲您完成一次轉換。然後使用鏈接循環並轉換每個項目。

像這樣:

public class Quote 
{ 
    public int Id { get; set; } 
    public string Type { get; set; } 
    public virtual ICollection<Rate> Rates { get; set; } 

    public static Quote FromData(Data.Quote input){ 
     if (input == null) return null; 
     Quote output = new Quote() 
     { 
      Id = input.QuoteId, 
      Type = input.Type 
     }; 
     output.Rates = (from i in input.RateSets 
         select Rate.FromData(i)).ToList(); 
    } 
} 

public class Rate 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Option> Options { get; set; } 

    public static Rate FromData(Data.RateSet input) 
    { 
     if (input == null) return null; 
     Rate output = new Rate() 
     { 
      Id = input.Id 
     }; 
     output.Options = (from i in input.Options 
          select Option.FromData(i)).ToList(); 
     return output; 
    } 
} 

public class Option 
{ 
    public int Id { get; set; } 
    public decimal Price { get; set; } 

    public static Option FromData(Data.Option input) 
    { 
     if (input == null) return null; 
     Option output = new Option() 
     { 
      Id = input.Id, 
      Price = input.Price ?? 0m 
     }; 
     return output; 
    } 
} 

namespace Data { 
    public class Quote 
    { 
     public int QuoteId { get; set; } 
     public string Type { get; set; } 
     public string Destination { get; set; } 
     public List<RateSet> RateSets { get; set; } 
    } 


    public class RateSet 
    { 
     public int Id { get; set; } 
     public decimal ValueMin { get; set; } 
     public decimal ValueMax { get; set; } 
     public List<Option> Options { get; set; } 
    } 

    public class Option 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public decimal? Price { get; set; } 
    } 
} 
0

我認爲你需要做的就是確定每兩個對應的類之間鑄造什麼,再投一個列表到另一個。

1

我會做多一點模塊化:

newQuotes = Quotes.Select(x => new Quote 
           { 
            ID = x.QuoteID, 
            Type = x.Type, 
            Rates = ConvertRates(x.RateSets) 
           }); 

ConvertRates將使用相同的方法來創建其子對象,可能是某個方法或Func

ICollection<Rate> ConvertRates(IEnumerable<RateSet> oldRates) 
{ 
    return oldRates.Select(x => new Rate 
           { 
            ID = x.ID, 
            Options = ConvertOptions(x.Options) 
           }).ToList(); 
} 

基本上,這與您使用的方法相同,只是分離和可讀。

相關問題