<commit_before>﻿using LcaDataModel;
using Repository.Pattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repository.Pattern.Repositories;
using Entities.Models;

namespace CalRecycleLCA.Repositories
{
    public static class LCIARepository
    {
        public static IEnumerable<LCIAModel> ComputeLCIA(this IRepositoryAsync<LCIA> repository,
            IEnumerable<InventoryModel> inventory, int lciaMethodId, int scenarioId)
        {
            return repository.Queryable()
                .Where(x => x.FlowID != null
                        && x.Geography == null
                        && x.LCIAMethodID == lciaMethodId)
                .Join(inventory,
                    l => l.FlowID,
                    i => i.FlowID,
                    (l, i) => new { l, i })
                .GroupJoin(repository.GetRepository<CharacterizationParam>().Queryable()
                        .Where(cp => cp.Param.ScenarioID == scenarioId), // Target table
                    l => l.l.LCIAID,
                    cp => cp.LCIAID,
                    (l, cp) => new { lcias = l, parameter = cp })
                .SelectMany(s => s.parameter.DefaultIfEmpty(),
                    (s, parameter) => new LCIAModel
                    {
                        FlowID = (int)s.lcias.l.FlowID,
                        DirectionID = s.lcias.l.DirectionID,
                        Quantity = (double)s.lcias.i.Result, // inventory table
                        Factor = parameter == null ? s.lcias.l.Factor : parameter.Value,
                        Geography = s.lcias.l.Geography,
                        CharacterizationParam = parameter == null ? null : new ParamInstance<commit_msg>Direction checking on LCIA join! toward #77<commit_after>﻿using LcaDataModel;
using Repository.Pattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repository.Pattern.Repositories;
using Entities.Models;

namespace CalRecycleLCA.Repositories
{
    public static class LCIARepository
    {
        public static IEnumerable<LCIAModel> ComputeLCIA(this IRepositoryAsync<LCIA> repository,
            IEnumerable<InventoryModel> inventory, int lciaMethodId, int scenarioId)
        {
            return repository.Queryable()
                .Where(x => x.FlowID != null
                        && x.Geography == null
                        && x.LCIAMethodID == lciaMethodId)
                .Join(inventory,
                    l => l.FlowID,
                    i => i.FlowID,
                    (l, i) => new { l, i })
                .Where(join => join.l.DirectionID == join.i.DirectionID)
                .GroupJoin(repository.GetRepository<CharacterizationParam>().Queryable()
                        .Where(cp => cp.Param.ScenarioID == scenarioId), // Target table
                    join => join.l.LCIAID,
                    cp => cp.LCIAID,
                    (join, cp) => new { lcias = join, parameter = cp })
                .SelectMany(s => s.parameter.DefaultIfEmpty(),
                    (s, parameter) => new LCIAModel
                    {
                        FlowID = (int)s.lcias.l.FlowID,
                        DirectionID = s.lcias.l.DirectionID,
                        Quantity = (double)s.lcias.i.Result, // inventory table
                        Factor = parameter == null ? s.lcias.l.Factor : parameter.Value,
                        Geography = s.lcias.l.Geography,
                        CharacterizationParam = parameter == null ? null : new ParamInstance
{'fraction_non_alphanumeric': 0.10101844205890449, 'fraction_numerical': 0.0005505092210294523, 'mean_word_length': 1.6162706983441324, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3104991', 'n_tokens_mistral': 994, 'n_tokens_neox': 932, 'n_words': 206}