我需要从单独的列表中提取两个值。我有一个Order对象列表和一个Visitors对象列表,每个对象按日期包含一个完整的对象列表。
我需要从OrdersList的VisitorsList和TotalOrders属性访问Visitors属性。然后我需要将每个TotalOrders与访问者分开以获得第三个值(转换率)。最后,我想将每个conversionRate值添加到List TotalConversions。
我正在尝试编写一个Linq查询,该查询从各自访问者和TotalOrders,但我不知道如何执行此操作。
List<GAVisitorsModel> VisitorsList = GetGAStatisticsReport(model);
List<GC_OrdersModel> OrdersList = GetOrderReport(model);
List<GC_ConversionRateModel> TotalConversions = new List<GC_ConversionRateModel>();
foreach (var order in OrdersList)
{
string _date = order.Date;
var Conversions = VisitorsList.Join(OrdersList, x => x.Visitors, y => y.TotalOrders, (x, y) => new { X = x, Y = y });
foreach (var c in Conversions)
{
//-- Y / X = C
//int _conversion = C;
//-- GC_ConversionRateModel GC_Orders = new GC_ConversionRateModel(_date, _conversion)
}
}
return TotalConversions;
列表TotalConversions应按日期返回转化列表。
这些是对象类:
GAVisitorsModel:
public class GAVisitorsModel : IGAVisitorsModel
{
public string Date { get; set; }
public int Visitors { get; set; }
public GAVisitorsModel(string _date, string _visitors)
{
Date = _date;
Visitors = _visitors;
}
}
GCOrdersModel:
public class GC_OrdersModel
{
public string Date { get; set; }
public int TotalOrders { get; set; }
public int TotalProducts {get; set;}
public GC_OrdersModel(string _date, int _totalOrders, int _totalProducts)
{
Date = _date;
TotalOrders = _totalOrders;
TotalProducts = _totalProducts;
}
}
GC_ConversionRateModel:
public class GC_ConversionRateModel
{
public string Date { get; set; }
public int ConversionRate { get; set; }
public GC_ConversionRateModel(string _date, int _conversionRate)
{
Date = _date;
ConversionRate = _conversionRate;
}
}
答案 0 :(得分:1)
我建议使用:
var result = from visitor in VisitorsList
join order in OrdersList on visitor.Date equals order.Date into orders
select new {
Visitor = visitor,
VisitorOrders = orders
};
假设您有一些“连接”访客和订单(Date
值)的属性,您将拥有相应订单的访问者列表。
答案 1 :(得分:1)
查看以下内容是否适用于您
public List<GC_ConversionRateModel> GetConversionRate()
{
List<GAVisitorsModel> VisitorsList = GetGAStatisticsReport(model);
List<GC_OrdersModel> OrdersList = GetOrderReport(model);
List<GC_ConversionRateModel> TotalConversions = new List<GC_ConversionRateModel>();
OrdersList.ForEach(o => {
TotalConversions.Add((from v in VisitorsList
where v.Date == o.Date
select new GC_ConversionRateModel(o.Date, o.TotalOrders / v.Visitors)).FirstOrDefault());
});
return TotalConversions;
}
此外,在GAVisitorsModel
中,当您尝试将字符串分配给Visitors = _visitors;
中ctor
的整数时,似乎出现了错误。