当我有超过2个元素时,如何改进列表下的添加项目

时间:2015-06-16 18:47:50

标签: c# performance linq entity-framework

下一个代码是可用的,但我想知道是否存在更好的方法来执行此功能。

使用linq我得到一个包含4个元素的列表,但我需要在ProposalItems列表中添加这些元素,下一个代码是列表的类型

private class ProposalItems
{
    public double Quantity { get; set; }
    public double Price { get; set; }
    public int Row { get; set; }
    public double Total { get; set; }
}

下一个代码是方法:

internal List<ProposalItems> GetProposalItems(int tradeId, int contendantId)
    {
        using (var db = new Entities())
        {
            //declare the list
            List<ProposalItems> items = new List<ProposalItems>();
            //query in linq
            var data = db.ES_SC_PropuestasPrecios
                .Join(db.ES_SC_CatalogoConceptos,
                prices => prices.renglon,
                concept => concept.renglon,
                (prices, concept) => new { ES_SC_PropuestasPrecios = prices, ES_SC_CatalogoConceptos = concept })
            .Where(w => w.ES_SC_CatalogoConceptos.idconcurso == tradeId && w.ES_SC_PropuestasPrecios.idconcursante == contendantId)
            //select the items
            .Select(s => new
            {
                Row = s.ES_SC_CatalogoConceptos.renglon,
                Quantity = s.ES_SC_CatalogoConceptos.cantidad,
                Price = s.ES_SC_PropuestasPrecios.preciounitario,
                Total = s.ES_SC_PropuestasPrecios.importe
            }).ToList();
            // loop to add the item in the list
            foreach (var item in data)
            {
                items.Add(new ProposalItems { Row = (int)item.Row, Price = (double)item.Price, Quantity = (double)item.Quantity, Total = (double)item.Total });
            }
            return items;
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试替换当前的选择

.Select(s => new ProposalItems { 
    Row = (int)s.ES_SC_CatalogoConceptos.renglon,, 
    Price = (double)s.ES_SC_PropuestasPrecios.preciounitario, 
    Quantity = (double)s.ES_SC_CatalogoConceptos.cantidad, 
    Total = (double)s.ES_SC_PropuestasPrecios.importe 
}).ToList();