c# - OData v4 filter query fails when using contains on calculated property -
so have code first ef 6 layer has contact class of:
public class contact { [key] public int id { get; set; } [maxlength(50)] public string prefix { get; set; } [maxlength(50)] public string suffix { get; set; } [maxlength(50)] public string firstname { get; set; } [maxlength(50)] public string middlename { get; set; } [maxlength(50)] public string lastname { get; set; } [notmapped] [displayname("full name")] public string fullname { { string tempname = (!string.isnullorempty(prefix) ? prefix + " " : "") + (!string.isnullorempty(firstname) ? firstname + " " : "") + (!string.isnullorempty(middlename) ? middlename + " " : "") + (!string.isnullorempty(lastname) ? lastname + " " : "") + (!string.isnullorempty(suffix) ? suffix + " " : ""); return tempname.trim(); } } [maxlength(50)] public string jobtitle { get; set; } public bool? primary { get; set; } public bool? inactive { get; set; } public int? customer_id { get; set; } [foreignkey("customer_id")] public virtual customer customer { get; set; } public virtual icollection<email> emails { get; set; } public virtual icollection<address> addresses { get; set; } public virtual icollection<phonenumber> phonenumbers { get; set; } public virtual icollection<note> notes { get; set; } }
i have asp.net web api 2 service running offers list of contacts, when perform odata query of $filter=contains(tolower(fullname), tolower('smith')) badrequest response. verified in webapi method getting results database, sends badrequest error.
it has fullname field either being calculated field or because has notmapped attribute. when change odata query $filter=contains(tolower(lastname), tolower('smith')) works fine. tried using display name of "full name" in query instead of "fullname" , did not work.
is there need make odata play nice calculated or notmapped field?
implement odata function on contactscontroller takes string comparison , returns filtered set of contacts. like:
[httpget] [odataroute("contacts/default.fullnamecontains(value={value})")] public ihttpactionresult fullnamecontains(string value) { value = value.tolower(); return ok(db.contacts.tolist().where(c => c.fullname.contains(value))); }
because fullname
computed, function must perform filtering in memory.
Comments
Post a Comment