C# 使用Linq将数据从一个集合传递到另一个集合
我想使用LINQ将数据从一个自定义集合传递到另一个自定义集合。它很复杂,因为集合有两个子集合 要将数据复制到:C# 使用Linq将数据从一个集合传递到另一个集合,c#,linq,C#,Linq,我想使用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
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 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; }
}
}
公共类报价
{
公共int Id{get;set;}
公共字符串类型{get;set;}
公共虚拟ICollection Rates{get;set;}
公共静态报价FromData(Data.Quote输入){
if(input==null)返回null;
报价输出=新报价()
{
Id=input.QuoteId,
Type=input.Type
};
output.Rates=(来自input.RateSets中的i)
选择Rate.FromData(i)).ToList();
}
}
公费
{
公共int Id{get;set;}
公共虚拟ICollection选项{get;set;}
来自数据的公共静态费率(Data.RateSet输入)
{
if(input==null)返回null;
速率输出=新速率()
{
Id=input.Id
};
output.Options=(来自input.Options中的i)
选择Option.FromData(i)).ToList();
返回输出;
}
}
公共类选项
{
公共int Id{get;set;}
公共十进制价格{get;set;}
来自数据的公共静态选项(Data.Option输入)
{
if(input==null)返回null;
选项输出=新选项()
{
Id=input.Id,
价格=输入。价格??0米
};
返回输出;
}
}
命名空间数据{
公开课报价
{
公共int QuoteId{get;set;}
公共字符串类型{get;set;}
公共字符串目标{get;set;}
公共列表速率集{get;set;}
}
公共类费率集
{
公共int Id{get;set;}
公共十进制值min{get;set;}
公共十进制值max{get;set;}
公共列表选项{get;set;}
}
公共类选项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制?价格{get;set;}
}
}
我认为您需要做的是在两个对应的类之间定义强制转换,然后将一个列表强制转换为另一个。我会使它更加模块化:
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();
}
i采集转换率(IEnumerable oldRates)
{
返回旧利率。选择(x=>新利率
{
ID=x.ID,
选项=转换选项(x.Options)
}).ToList();
}
基本上,这与您使用的方法相同,只是拆分并可读。编译时没有任何错误
// 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();
}
//到
公共类引用2
{
公共int Id{get;set;}
公共字符串类型{get;set;}
公共虚拟ICollection Rates{get;set;}
}
公费
{
公共int Id{get;set;}
公共虚拟ICollection选项{get;set;}
}
公共类选项2
{
公共int Id{get;set;}
公共十进制价格{get;set;}
}
//从
公开课报价1
{
公共int QuoteId{get;set;}
公共字符串类型{get;set;}
公共字符串目标{get;set;}
公共列表速率集{get;set;}
}
公共类费率集
{
公共int Id{get;set;}
公共十进制值min{get;set;}
公共十进制值max{get;set;}
公共列表选项{get;set;}
}
公共类选项1
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制?价格{get;set;}
}
void Main()
{
var Quotes=新列表();
var newQuotes=Quotes
.选择(x=>新报价2{
Id=x.QuoteId,
Rates=x.RateSets==null?null:x.RateSets。选择(y=>new Rate{
Id=y.Id,
选项=y。选项==null?null:y。选项。选择(z=>newoption2{
Id=z.Id,
价格=z.Price.Value
}).ToList()}.ToList()}.ToList();
}
“不断遇到问题…”一定要告诉我。你有什么样的问题?这对我很有用,而one LINQ语句正是我努力的方向。@FloatLeft:你应该重新思考一下。这个linq语句很难维护和阅读。
ICollection<Rate> ConvertRates(IEnumerable<RateSet> oldRates)
{
return oldRates.Select(x => new Rate
{
ID = x.ID,
Options = ConvertOptions(x.Options)
}).ToList();
}
// 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();
}