Beginner
Beginner

Reputation: 29543

linq query how to take out spaces in strings

I am trying to match a string typed in by a user to a string in a database. Before I try match them i need to take the spaces out both of them.

how do i do this?

  public static Product GetProductbypart(ModelContainer context, string partnumber)
    {
        var query = from product in context.Products
                    where product.Partnumber == partnumber
                    select product;

        return query.FirstOrDefault();
    }

this is my query which works if the user types in the exact part number. But some users may type it in with too many spaces or too less.

I want to take the partnumber take out the spaces. Then take the product.Partnumber and take of the spaces of that also, to see if there is a match.

Sample inputs:

MC-9a 1a AC24V
MC-9a 50/60Hz
1
123
MC+123-1
F6h67e
_8jj+j7s  

Upvotes: 1

Views: 4470

Answers (4)

Snowbear
Snowbear

Reputation: 17274

In this case I would think about implementing it a little bit differently. If you need your table only in order to compare it with the user input, you can remove spaces in the database in advance, before running the query. Also there is no point in removing spaces in database, you can do it in client code.

In this case you will be able to use regular Equals and also it should be a little bit more efficient.

Upvotes: 1

Beginner
Beginner

Reputation: 29543

   public static Product GetProductbypart(ModelContainer context, string partnumber)
            {
                partnumber = partnumber.Replace(" ", String.Empty);
                var products = from product in context.Products
                               select product;
                foreach(var item in products)
                {
                    if (item.Partnumber != null)
                    {
                        item.Partnumber = item.Partnumber.Replace(" ", String.Empty);
                        if (item.Partnumber == partnumber)
                        {
                            var query = from product in context.Products
                                        where product.Id == item.Id
                                        select product;

                            return query.FirstOrDefault();
                        }
                    }
                }    
                return null;                
            }

This is how i did it

Upvotes: 2

tbddeveloper
tbddeveloper

Reputation: 2447

I think the best answer here is to not try and figure out how the user entered the data, instead make sure that they entered a valid part number in the first place. Can you provide some form of input format? Even if it means using hyphens where spaces are, you can always remove them before you check, but at least you'll have data in the correct format.

Upvotes: 0

Stefan
Stefan

Reputation: 14880

string partnumber = partnumber.Replace(" ", String.Emtpy);
var query = from product in context.Products
                    where product.Partnumber.Replace("", String.Empty) == partnumber
                    select product;

This removes spaces in the strings product.Partnumber and partnumber. However, if you use linq-to-SQL the part product.Partnumber.Replace(..) won't work. But I'm not sure why you have to remove spaces of the product number in the database. Sounds like inconsistent data to me.

Upvotes: 2

Related Questions