Skip to content

Including standard functions in search criteria

object edited this page Oct 4, 2012 · 14 revisions

OData protocol supports standard functions that can be included in search criteria.

Find a product with the given name coverted to a lowercase format:

var product = _db.Products.Find(_db.Products.ProductName.ToLower() == "chai");
Assert.Equal("Chai", product.ProductName);

Find a product with the length of name equal to 4:

var product = _db.Products.Find(_db.Products.ProductName.Length() == 4);
Assert.NotEmpty(products);

Find a product with the name starting with the given string:

var product = _db.Products.Find(_db.Products.ProductName.StartsWith("Ch") == true);
Assert.NotEmpty(products);

Find a product with the name containing the given string:

var product = _db.Products.Find(_db.Products.ProductName.Contains("ai") == true);
Assert.NotEmpty(products);

Find a product with the name not containing the given string:

var product = _db.Products.Find(_db.Products.ProductName.Contains("ai") == false);
Assert.NotEmpty(products);

Find a product with the name containing the given string at the specified position:

var product = _db.Products.Find(_db.Products.ProductName.IndexOf("ai") == 2);
Assert.NotEmpty(products);

Alternative syntax for finding a product with the name containing the given string at the specified position:

var product = _db.Products.Find(_db.Products.ProductName.Substring(1) == "hai");
Assert.NotEmpty(products);

See also:
OData URI conventions