Json returning string in wrong format - model-view-controller

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}

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());
}

Dynamic Linq Any syntax

I am trying to figure out how to use Any operator in Linq.Dynamic, here is my test code:
var bar = new[] {
new {Bar = "A", Id = 1},
new {Bar = "B", Id = 1},
new {Bar = "C", Id = 2},
new {Bar = "A", Id = 2},
new {Bar = "B", Id = 3},
new {Bar = "C", Id = 3}
};
var foo = new[] {
new {Foo = 1, Id = 1},
new {Foo = 1, Id = 1},
new {Foo = 2, Id = 2},
new {Foo = 2, Id = 2},
new {Foo = 2, Id = 3},
new {Foo = 3, Id = 3}
};
var res = foo.GroupJoin(bar, x => x.Id, y => y.Id, (x, l) => new { F = x.Foo, Bs = l.Select(b => b.Bar).ToArray() }).ToArray();
var normalLinq = res.Where(x => x.Bs.Any(y => y == "A")).ToArray();
var dynamicLinq = res.AsQueryable().Where("Bs.Any(x => x==\"A\")").ToArray(); //won't work
The syntax "Bs.Any(x => x==\"A\")" is wrong as dynamic linq doesn't seem to know the lambda expression I put in, and exception is thrown. How do I make it work?
My goal is to parse dynamic search input with this code. If you Linq experts have other better ways to do dynamic search, please suggest?
If you are using this library: http://www.nuget.org/packages/System.Linq.Dynamic/ as far as I know it does not allow lambda expressions. However, there is somebody who extended this library. I have never tried it but it looks promising. http://dynamiclinq.codeplex.com/ Good luck :)

Predicate builder contains

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()));

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