LIKE operator in LINQ

前端 未结 14 1160
一生所求
一生所求 2020-11-27 16:29

Is there any way to compare strings in a C# LINQ expression similar to SQL\'s LIKE operator?

Suppose I have a string list. On this list I want to search

14条回答
  •  孤街浪徒
    2020-11-27 16:45

    Like Extension Linq / SQL

    LikeExtension Class

    Tested in .NET 5

     public static class LikeExtension {
    
        private static string ColumnDataBase(IModel model, Expression> predicate) where TEntity : class {
    
            ITable table = model
                .GetRelationalModel()
                .Tables
                .First(f => f
                    .EntityTypeMappings
                    .First()
                    .EntityType == model
                    .FindEntityType(predicate
                        .Parameters
                        .First()
                    .Type
                ));
    
            string column = (predicate.Body as MemberExpression).Member.Name;
            string columnDataBase = table.Columns.First(f => f.PropertyMappings.Count(f2 => f2.Property.Name == column) > 0).Name;
    
            return columnDataBase;
    
        }
    
        public static IQueryable Like(this DbContext context, Expression> predicate, string text) where TEntity : class {
    
            string columnDataBase = ColumnDataBase(context.Model, predicate);
            return context.Set().FromSqlRaw(context.Set().ToQueryString() + " WHERE [" + columnDataBase + "] LIKE {0}", text);
    
        }
    
        public static async Task> LikeAsync(this DbContext context, Expression> predicate, string text, CancellationToken cancellationToken) where TEntity : class {
    
            string columnDataBase = ColumnDataBase(context.Model, predicate);
            return await context.Set().FromSqlRaw(context.Set().ToQueryString() + " WHERE [" + columnDataBase + "] LIKE {0}", text).ToListAsync(cancellationToken);
    
        }
    
        public static async Task> LikeAsync(this IQueryable query, Expression> predicate, string text, CancellationToken cancellationToken) where TEntity : class {
    
            DbSet entities = query as DbSet;
            string columnDataBase = ColumnDataBase(entities.EntityType.Model, predicate);
            return await entities.FromSqlRaw(query.ToQueryString() + " WHERE [" + columnDataBase + "] LIKE {0}", text).ToListAsync(cancellationToken);
    
        }
    
        public static IQueryable Like(this IQueryable query, Expression> predicate, string text) where TEntity : class {
    
            DbSet entities = query as DbSet;
            string columnDataBase = ColumnDataBase(entities.EntityType.Model, predicate);
            return entities.FromSqlRaw(query.ToQueryString() + " WHERE [" + columnDataBase + "] LIKE {0}", text);
    
        }
    
    }
    

    Repository

        public async Task> LikeAsync(Expression> predicate, string text, CancellationToken cancellationToken) {
    
            return await context.LikeAsync(predicate, text, cancellationToken);
    
        }
    
        public IQueryable Like(Expression> predicate, string text) {
    
            return context.Like(predicate, text);
    
        }
    

    Use

     IQueryable result = countryRepository
         .Like(k => k.Name, "%Bra[sz]il%") /*Use Sync*/
         .Where(w => w.DateRegister < DateTime.Now) /*Example*/
         .Take(10); /*Example*/
    

    Or

     IEnumerable result = await countryRepository
         .LikeAsync(k => k.Name, "%Bra[sz]il%", cancellationToken); /*Use Async*/
    

    Or

     IQueryable result = context.Countries
         .Like(k => k.Name, "%Bra[sz]il%")
         .Where(w => w.Name != null); /*Example*/
    

    Or

     List result2 = await context.Countries
         .Like(k => k.Name, "%Bra[sz]il%")
         .Where(w => w.Name != null) /*Example*/
         .ToListAsync(); /*Use Async*/
    

    Or

     IEnumerable result3 = await context.Countries
         .Where(w => w.Name != null)
         .LikeAsync(k => k.Name, "%Bra[sz]il%", cancellationToken); /*Use Async*/
    

提交回复
热议问题