Domanda

I am facing CA1506 code analysis warning for following code

private void ShowProductStatistics(object obj)
    {
        this.currentProduct = obj as Products;
        Task.Factory.StartNew(() =>
        {
            var topOrderQuery = (from orderDetail in new XPQuery<OrderDetails>(new Session())
                                 where
                                     orderDetail.ProductID.ProductID == currentProduct.ProductID
                                 orderby
                                     (orderDetail.UnitPrice * orderDetail.Quantity) descending
                                 select new TopOrder
                                 {
                                     OrderId = orderDetail.OrderID.OrderID,
                                     TotalSales = orderDetail.UnitPrice * orderDetail.Quantity
                                 }).ToList().Take(10);

            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.TopProduct = topOrderQuery; }));

            var orderPerYearQuery = (from order in new XPQuery<OrderDetails>(new Session())
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new OrderPYear
                                         {
                                             TotalOrder = g.Count(),
                                             OrderYear = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.OrderPerYear = orderPerYearQuery; }));

            var salesPerYearQuery = (from order in new XPQuery<OrderDetails>(new Session())
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new SalesPYear
                                         {
                                             Sales = g.Sum(p => p.UnitPrice * p.Quantity),
                                             Year = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.SalesPerYear = salesPerYearQuery; }));
        });
    }

I tried to solve this warning by following suggestions given in the msdn but not succeeded. Can anybody help me to solve this warning ??

Thanks & regards, Rudresh

È stato utile?

Soluzione

Change the code as follows.

this.currentProduct = obj as Products;
        List<OrderDetails> orderDetailLIst = new XPQuery<OrderDetails>(new Session()).ToList();
        Task.Factory.StartNew(() =>
        {
            var topOrderQuery = (from orderDetail in orderDetailLIst
                                 where
                                     orderDetail.ProductID.ProductID == currentProduct.ProductID
                                 orderby
                                     (orderDetail.UnitPrice * orderDetail.Quantity) descending
                                 select new TopOrder
                                 {
                                     OrderId = orderDetail.OrderID.OrderID,
                                     TotalSales = orderDetail.UnitPrice * orderDetail.Quantity
                                 }).ToList().Take(10);

            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.TopProduct = topOrderQuery; }));

            var orderPerYearQuery = (from order in orderDetailLIst
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new OrderPYear
                                         {
                                             TotalOrder = g.Count(),
                                             OrderYear = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.OrderPerYear = orderPerYearQuery; }));

            var salesPerYearQuery = (from order in orderDetailLIst
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new SalesPYear
                                         {
                                             Sales = g.Sum(p => p.UnitPrice * p.Quantity),
                                             Year = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.SalesPerYear = salesPerYearQuery; }));
        });

The cause of warning was "from orderDetail in new XPQuery(new Session())" statement which I was using 3 times therefore it was giving warning. I tried to reduce coupling.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top