How to make a LIKE in Linq?

1

I will reformulate the question better: How can I do a like in linq?

    
asked by Luis Gabriel Fabres 22.09.2017 в 23:31
source

1 answer

3

Have you already tried the .Contains extension of LINQ? When you use '==' the "description" must be equal (in your case) with the value of the Descriptino variables, however with .Contains it searches within the Description something equal to the value of the Description variable, so it will not be It is necessary to look for the complete description in order to filter it, it would be something like:

private List<ProductDto> _GetProductByDescription(string Description)
{
    WS_ProductRepository productRepo = new WS_ProductRepository();
    List<WS_Product> lstProducts = productRepo.Filter(x => x.Description.Contains(Description));
    if (lstProducts != null && lstProducts.Count > 0)
    {
        List<ProductDto> lstProductDto = new List<ProductDto>();
        foreach (WS_Product product in lstProducts)
        {
            ProductDto productDto = new ProductDto();
            productDto.ProductId = product.ProductId;
            productDto.Description = product.Description;
            productDto.CategoryId = product.CategoryId;
            productDto.Price = product.Price;
            lstProductDto.Add(productDto);
        }
        return lstProductDto;
    }
    return null;
}
    
answered by 22.09.2017 / 23:41
source