// used for to GetTextSearchWheres private static string GetTextSearchWhere(TextSearchProperty fullTextProperty, string wordToSearch) { string result = fullTextProperty.EntityProperty; if (fullTextProperty.MustCallToString) { result += ".ToString()"; } result += ".Contains(\"" + wordToSearch + "\")"; return(result); }
// use entity props with [TextSearch] as default static public List <string> GetTextSearchWheres <TEntity>(string TextSearch) where TEntity : IEntity { // TODO add a way to // prepara una lista speciale delle propieta dell'entita con l'attributo [TextSearch] // build special list of entity properties with attribute [TextSearch] List <TextSearchProperty> textSearchProperties = new(); PropertyInfo[] entityProps = typeof(TEntity).GetProperties(); foreach (var entityProp in entityProps) { var att = entityProp.GetCustomAttribute(typeof(TextSearchAttribute)); if (att != null) { var allowedTypes = TEXT_SEARCH.ALLOWED_TYPES; if (!allowedTypes.Contains(entityProp.PropertyType)) { throw new NotSupportedException("entityProp.PropertyType are not allowed for [TextSearch]"); } var textSearchProperty = new TextSearchProperty(entityProp.Name, entityProp.PropertyType != typeof(string)); textSearchProperties.Add(textSearchProperty); } } // split and trim TextSearch in words var textSearchWords = TextSearch.Split(" ").Select(e => e.Trim()); // TODO improve namings // loop to build where clausole for every textSearchWord List <string> wordsWhere = new(); foreach (var textSearchWord in textSearchWords) { string wordWhere = GetTextSearchWhere(textSearchProperties.First(), textSearchWord); foreach (var col in textSearchProperties.Skip(1)) { wordWhere += "|| " + GetTextSearchWhere(col, textSearchWord); } wordsWhere.Add(wordWhere); } return(wordsWhere); }