c# - LINQ to Entities create dynamic field -
i trying create dynamic field using linq entities w/ ef5. based on conditions (which i've encapsulated in function) dynamic field populated different values.
i referenced post here when run code below, follow error:
linq entities not recognize method 'system.string formatname()' method, , method cannot translated store expression.
public static ienumerable<dynamic> selectallcustomers() { using (var db = new databasecontext()) { var query = db.customer.select(c => new { c.id, fullnamelastfirstmiddle = formatname(c.first_name, c.middle_name, c.last_name), } ); return query.tolist(); } } private static string formatname(string first, string middle, string last) { //format name if (!string.isnullorwhitespace(first)) { if (!string.isnullorwhitespace(middle)) return last + ", " + first + " " + middle; else return last + ", " + first; } else { if (!string.isnullorwhitespace(middle)) return last + ", " + middle; else return last; } }
any ideas on how best dynamically build field sofisticated logic using linq entities?
string formatting doesn't need translated sql , performed on database side in first place. instead query database information need , perform string manipulation on application side using linq objects:
var query = db.customer.select(c => new { c.id, c.first_name, c.middle_name, c.last_name, }) .asenumerable() .select(c => new { c.id, fullnamelastfirstmiddle = formatname(c.first_name, c.middle_name, c.last_name), });
Comments
Post a Comment