Predicate builder contains - asp.net-mvc-3

I am trying to execute query through MVC3 with predicate builder. I am getting the error
"Unable to cast the type 'System.Boolean' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types."
I have query passed as below
if (!string.IsNullOrEmpty(SearchObj.EmployeeIDSortId.ToString()) && SearchObj.EmployeeIDSortId > 0)
{
string[] vals =new string[' '];
if (SearchObj.EmployeeIDSortId == 1)
{
vals = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l" };
}
else if (SearchObj.EmployeeIDSortId == 2)
{
vals = new string[] { "m", "n", "o", "p", "q", "r", "s" };
}
else if (SearchObj.EmployeeIDSortId == 3)
{
vals = new string[] { "t", "u", "v", "w", "x", "y", "z" };
}
employeepredicate =
employeepredicate.And(emp => emp.EmployeeName.Substring(0, 1).ToLower().Equals(vals.Any()));
}
Can you please suggest what is wrong with this ,I guess it is only due to my query vals.Any().
Thanks

Let me preface - I'm unfamiliar with the And method, but the .Equals(vals.Any()) doesn't look right to me. So, I assume that And takes a function returning a bool. What you're really looking for is to find out if the first character in the employee name is contained in the vals array:
employeepredicate = employeepredicate.And(
emp => vals.Contains(emp.EmployeeName.Substring(0, 1).ToLower()));

Related

How to print all the ways to cut off a string - combination

input: "abcd"
output: a list of all the ways to cut off the string.
[ ["a", "bcd"],["a", "b", "cd"], ["a", "b", "c", "d"],
["ab", "cd"], ["ab", "c", "d"],
["abc", "d"],
["abcd", ""]
]
I want a recursive solution. Preferably Java but not need to be language specific.
Thank you!
Please notice "abd" "c" is not valid, because you cannot rearrange the order.
There are a lot of ways to solve this problem. Here is one of them
import java.util.ArrayList;
import java.util.List;
public class Splitter {
public static void main(String[] args) {
final String string = "abcd";
//You should add empty string as a base
printAllSplits(string, 0, new ArrayList<>(List.of(new StringBuilder())));
}
static void printAllSplits(String s, int currentIndex, List<StringBuilder> list) {
if (currentIndex == s.length()) { //reached the end, nothing left to split
System.out.println(list);
return;
}
//We should consider two decisions at each index: 1. split
// 2. don't split
//split here
if (currentIndex > 0) {
list.add(new StringBuilder(Character.toString(s.charAt(currentIndex))));
printAllSplits(s, currentIndex + 1, list);
//remove it not to affect a future call
list.remove(list.size() - 1);
}
//don't split here
list.get(list.size() - 1).append(s.charAt(currentIndex));
printAllSplits(s, currentIndex + 1, list);
}
}
You can modify this code to fit your needs. If you don't like or understand anything, feel free to ask. Also, if you need exactly a string, you can convert string builder to string at the end. I use string builder, because it's mutable.

How to create a set in Ballerina?

I have a requirement to create a set of strings using an array of strings in Ballerina. Other than using reduce() or checking indexOf() before inserting, I couldn't find a way to create a set using Ballerina programming language.
string[] arr = ["a", "b", "c", "a", "d", "c"];
string[] set = arr.reduce(function(string[] accum, string item) returns string[] {
if accum.indexOf(item) == () {
accum.push(item);
}
return accum;
}, []);
Is there a better way to create a set in Ballerina?
Since what you want is just set of strings, I would suggest to use a map.
import ballerina/io;
public function main() {
string[] s = ["a", "b", "a"];
map<()> m = {};
foreach var i in s {
m[i] = ();
}
string[] unique = m.keys();
io:println(unique);
}
For set of complex type, we can go with a table and do something similar to what we did with map.
If the intended members of the set are not strings; we can use a table instead. Below is an example
import ballerina/io;
type TblConstraint record {|
//can use a type that is a subtype of anydata
readonly anydata keyValue;
|};
public function main() {
int[] arr = [1, 2, 3, 3, 4, 4, 5];
table<TblConstraint> key(keyValue) setImpl = table [];
foreach int item in arr {
if (!setImpl.hasKey(item)) {
setImpl.put({keyValue: item});
}
}
//unique values as in a set
io:println(setImpl.keys());
}

Json returning string in wrong format

public ActionResult BusinessUnitNameSelect(BusinessUnitDTO business)
{
BOSetup setup = new BOSetup();
string newBusiness = setup.BusinessUnitNameSelect(business);
return Json(newBusiness, JsonRequestBehavior.AllowGet);
}
Json returning string in this format. any solution to get string from json in actual format? Actual format is
General
{0: "G", 1: "e", 2: "n", 3: "e", 4: "r", 5: "a", 6: "l", $promise: Promise, $resolved: true}

How do I add a value to a nested hash?

I have a nested hash:
hash = {
"a" => "a",
"b" => {
"c" => "c",
"d" => {
"e" => "e"
}
}
}
and I have a hash:
new_value = {
"b.d.e" => "new value"
}
I need some sort of "magical" function that replaces the value of the hash at hash["b"]["d"]["e"], like:
magical_function(hash, new_value)
#=> hash = {
"a" => "a",
"b" => {
"c" => "c",
"d" => {
"e" => "new value"
}
}
}
I have no idea how. Can someone help please?
It's not magical if it's implemented in a straight-forward manner:
merge_hash.each do |key, value|
parts = key.split('.')
leaf = parts.pop
target = parts.inject(hash) do |h, k|
h[k] ||= { }
end
target[leaf] = value
end
Here's another solution:
class Hash
def replace_value(*keys, value)
current = self
current = current[keys.shift] while keys.size > 1
current[keys.last] = value
end
end
Called by invoking hash.replace_value("b","d","e", "new_value").

Asp.Net Membership Password Resets without symbols

I'm using the asp.net Membership Provider in an application.
I'm also using the "out of the box" asp:PasswordRecovery control.
My client is complaining that the new passwords being issued are too complicated.
e.g. }>;-(hYrS^OTfY
Are there any small tweaks I can make so the new passwords only contain letters and numbers?
thanks!
This worked:
using System;
using System.Collections.Generic;
using System.Web.Profile;
using System.Web.Security;
using System.Text;
namespace TS.Common.MembershipProvider
{
public class MembershipProvider : SqlMembershipProvider
{
/// Create an array of characters to user for password reset.
/// Exclude confusing or ambiguous characters such as 1 0 l o i
string[] characters = new string[] { "2", "3", "4", "5", "6", "7", "8",
"9", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
const int DefaultResetPasswordLength = 10;
private int ResetPasswordLength;
// Create a more user friendly password to avoid confusion when
// trying to key in the new value
public override string GeneratePassword()
{
string newPassword = string.Empty;
System.Random rnd = new Random();
for (int i = 0; i < ResetPasswordLength; i++)
{
newPassword +=
characters[rnd.Next(characters.GetUpperBound(0))];
}
return newPassword;
}
}
}

Resources