Esempio n. 1
0
        public async Task <ActionResult> GetFarmSamples(string agreementNumber)
        {
            if (string.IsNullOrWhiteSpace(agreementNumber))
            {
                return(new HttpStatusCodeResult(400));
            }

            var supplier = UserManager.GetActiveCustomer(HttpContext);

            if (string.IsNullOrWhiteSpace(supplier.CustomerNo))
            {
                return(HttpNotFound());
            }

            var listPriceHedgings = await _agreementRepository.GetFarmSamplesListAsync(supplier.CustomerNo, agreementNumber);

            var groupOfAnalysis = listPriceHedgings
                                  .Select(x => new FarmSample {
                Analyskod = x.Analyskod, Resultat = x.Resultat, ProvNr = x.ProvNr
            })
                                  .Distinct()
                                  .ToList();

            // list all ProvNr
            var listProvNrs = groupOfAnalysis.Select(x => x.ProvNr).Distinct().OrderBy(x => x);

            var listItemByKodAndProvNr = groupOfAnalysis.GroupBy(x => new { x.Analyskod, x.ProvNr })
                                         .Select(y => new FarmSample
            {
                Analyskod = y.Key.Analyskod,
                ProvNr    = y.Key.ProvNr,
                Resultat  = y.Select(a => a.Resultat).FirstOrDefault()
            }).OrderBy(x => x.Analyskod).ThenBy(x => x.ProvNr).ToList();

            var dictResultByAnalyskod = new Dictionary <string, List <string> >();

            foreach (var item in listItemByKodAndProvNr)
            {
                dictResultByAnalyskod.AddToDictionary(item.Analyskod, item.Resultat);
            }

            var viewModel = new AgreementAnalyzeViewModel
            {
                AgreementNumber       = agreementNumber,
                AnalysisProvNrs       = listProvNrs,
                DictResultByAnalyskod = dictResultByAnalyskod
            };

            return(PartialView("Dialogs/AnalyzeBody", viewModel));
        }