clickatwill
clickatwill

Reputation: 980

LINQ to SQL matching using possible wildcard

I'm using LINQ to SQL to find a record in the databse that has a name which matches a string variable. This variable's value comes in from the URL and has any space replaced with a dash for SEO purposes: mysite.com/what-i-need-to-find

It's easy enough to replace the dashes with spaces to do the lookup, but what if the record I'm looking for actually has a dash in the name?

For example, the record I'm looking for is named "John to-do list" and I'm working with the value "John-to-do-list"

var Item = Items.List().FirstOrDefault(i => i.Name == "John to do list"); //obviously doesn't get what I need

Is there any way to get back this item using wildcards or some other method?

Upvotes: 0

Views: 280

Answers (2)

clean_coding
clean_coding

Reputation: 1166

I would suggest you avoid changing spaces to a character that's legal in your URL. The way I would solve this, is to have an alias in my database, for example.

You have a title of a note, and you want to display it in the url. The title is "My to-do list for today" and is stored in a column in the database. I would then add a second column, to store whatever i want to be displayed in the url (could also index this). For example "my-to-do-list-for-today", and you would only have to parse once when creating the item.

Upvotes: 0

Ryan Schlueter
Ryan Schlueter

Reputation: 2221

I would try something like this. var Item = Items.List().FirstOrDefault(i => i.Name.Like("%John%to%do%list%"));

Upvotes: 0

Related Questions