using Datamodels.BusinessModels; using Datamodels.DatabaseModels; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Repositories.Interfaces; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Linq; using Microsoft.EntityFrameworkCore; using Datamodels.SearchModels; using System.Linq.Expressions; using System.Diagnostics; namespace Repositories { public class FwMariaSearchRepo : IFwMariaSearchRepo { private readonly FwDbContext _dbContext = new FwDbContext(); private readonly ILogger _logger; private readonly IConfiguration _configuration; public FwMariaSearchRepo(ILogger logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; try { if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development") { _dbContext._connectionString = "SERVER=127.0.0.1;DATABASE=testdb;PORT=3306;USER=root;PASSWORD=example"; } else { string usedDb = configuration.GetSection("GeneralSettings").GetSection("DbToUse").Get(); string connectionString = configuration.GetConnectionString(usedDb); _dbContext._connectionString = connectionString; } } catch (Exception ex) { _logger.LogError(ex, "Database Connection Configuration not valid"); } } public async Task>> GetAllAddresses() { try { var result = await _dbContext.Address.Where(x => x.City == "Augsburg" || x.City == "München").Where(y => y.StreetName.Contains("bruck")).ToListAsync().ConfigureAwait(false); return new DataResult> { Data = result }; } catch (Exception ex) { return new DataResult> { GeneralResult = GeneralResults.DatabaseError, UserMessage = "Fehler beim Laden der Adressen", ServerMessage = ex.Message }; } } public Task GetAllSortedData(TableModel queryFieldDef, List sortModel) { throw new NotImplementedException(); } public Task GetBlobData(string tableName, string fieldName, long id) { throw new NotImplementedException(); } public Task GetDataFilteredAndSorted(List filter, List sortModel, long skip = 0, long take = -1) { throw new NotImplementedException(); } public async Task>> GetFilteredData(List expressions, long skip = 0, long take = -1) where T : IEntityClass { var where = FilterLinq.GetWherePredicate(wherefield, wherefieldvalue).Compile(); items = await _dbContext.Address.Where((Expression>)where).ToListAsync(); var result = new DataResult>(); if (typeof(T).Equals(typeof(Address))) { Expression combined = null; foreach (var exp in expressions) { if (combined == null) { combined = exp; } else { combined = Expression.Or(combined, exp); } } ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name); combined = Expression.Lambda>(combined, new ParameterExpression[] { pe }); //var addressFound = await _dbContext.Address.Where((Expression>)combined).ToListAsync(); var addressFound = await _dbContext.Address.Where(a => a.City == "Augsburg" | a.City == "München").ToListAsync(); result.Data = new List(addressFound.Cast()); } return result; } } public class FilterLinq { public static Expression> GetWherePredicate(string whereFieldList, string whereFieldValues) { //the 'IN' parameter for expression ie T=> condition ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name); //combine them with and 1=1 Like no expression Expression combined = null; if (whereFieldList != null) { string[] field = whereFieldList.Split(';'); string[] fieldValue = whereFieldValues.Split(';'); for (int i = 0; i < field.Count(); i++) { //Expression for accessing Fields name property Expression columnNameProperty = Expression.Property(pe, field[i]); //the name constant to match Expression columnValue = Expression.Constant(fieldValue[i]); //the first expression: PatientantLastName = ? Expression e1 = Expression.Equal(columnNameProperty, columnValue); if (combined == null) { combined = e1; } else { combined = Expression.And(combined, e1); } } } //create and return the predicate return Expression.Lambda>(combined, new ParameterExpression[] { pe }); } } }