Proper way to transform column and table names in SubSonic 3 - subsonic

I am attempting to transform a table named app_user which has a column named created_dt into AppUser.CreatedDt in SubSonic3 using the ActiveRecord template. From what I've seen one should be able to modify table and column names as needed in the CleanUp method of Settings.ttinclude
So I added this method to Settings.ttinclude
string UnderscoreToCamelCase(string input) {
if( !input.Contains("_"))
return input;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '_')
{
while (i < input.Length && input[i] == '_')
i++;
if (i < input.Length)
sb.Append(input[i].ToString().ToUpper());
}
else
{
if (sb.Length == 0)
sb.Append(input[i].ToString().ToUpper());
else
sb.Append(input[i]);
}
}
return sb.ToString();
}
And then this call to CleanUp
result=UnderscoreToCamelCase(result);
If I run a query such as :
var count = (from u in AppUser.All()
where u.CreatedDt >= DateTime.Parse("1/1/2009 0:0:0")
select u).Count();
I get a NotSupportedException, The member 'CreatedDt' is not supported
which comes from a method in TSqlFormatter.sql line 152
protected override Expression VisitMemberAccess(MemberExpression m)
If I comment out the call to UnderscoreToCamelCase and use the names as they are in the database everything works fine.
One interesting thing is that when everything is working OK the VisitMemberAccess method is never called.
Has anyone else been able to convert table/column names with undersores in them to camel case in SubSonic3 ?

There may be an answer to this on another thread within StackOverflow, but it entails modifying the source code for Subsonic.core.
link text

Related

Make switch case as a generic case

I have more than 50 cases in switch statement. I don't want to write each case one by one and all cases are doing the same work. I want to make a generic case which do the work for all. This code is for unity. I want to know how we used case as a generic?
Basically this code is for displaying image one by one per clicks mean per click it shows the different image. Please help me how can I make it generic. It give error in case 'i'.
Here is my code:
for (int j = 0; j != Gallery.Length; j++)
{
switch (i)
{
*case 'i':*
displayimage.sprite = Gallery[i];
i++;
break;
default:
Debug.Log("Muzaffar");
break;
}
}
Here example:
Sprite[] Gallery;
//SelectedSprite - can be string type variable, for selecting case.
Sprite SelectedSprite;
for (int i = 0; i < Gallery.Length; i++)
{
displayimage.sprite = SelectedSprite.name == Gallery[i].name ? Gallery[i].sprite : null;
}

How to make the return false if the arraylist already have the string present in class?

I'm new to coding.
How do I return a false if there is a string being added that's already in the arraylist?
For example, if you have a list of dog names in the class and you add new dog names in the list, but don't add it when the same dog name was already in the list?
The Solution:
You could use a for statement to iterate through your array list:
public static bool checkArray(string dogName)
{
for int i=0; i<arrayName.Length; i++) // basic for loop to go through whole array
{
if (arrayName[i] == dogName) //checks if array value at index i is the dog's name
{
return true; //if it is, return true
}
}
return false; //gone through whole array, not found so return false
}
This means you can call your method via
string Name = "myDogsName";
bool isAlreadyPresent = checkArray(Name);
Note
This is written in C#, and so other coding languages will slightly
differ in their syntax.
isAlreadyPresent will then contain a bool value if the dog is
present or not
I have written this (for learning purposes) in (possibly) an
inefficient way, but should allow you to understand what is happening
at each stage.
the i++
The i++ may confuse new programmers, but effectively it is the same as writing
i = i + 1;
This also works for i--;
i = i - 1;
Or even i*=2;
i = i * 2;

how to read the content of file and store it in formatted array in c#

I am trying to read text from file, checking its content and then storing it in an array of string.
FileStream fs = new FileStream(pathToFiles, FileMode.Open);
StreamReader sr = new StreamReader(fs);
do{
line=sr.ReadLine();
if (line == "databases")
{
j = 0;
while ((ch = sr.Read()) != '}')
{
admin_databases[j] = sr.ReadLine();
j++;
}
}
else if (line == "table_name")
{
j = 0;
while ((ch = sr.Read()) != '}')
{
admin_table_name[j] = sr.ReadLine();
j++;
}
}
else
{
Response.Write(line+" ");
}
} while (line !=null);
The text is read by using ReadLine() method, but while checking its content
i.e
if(line=="databases")
it shows null string and hence unable to store it in an array.
what is the mistake that i am making here?
As an answer to last comments under main post :
According to what you say, we're back to whitespace theory !
A few tips :
Replace Response.Write(line+" "); by Response.Write("'" + line+"'"); just to see exact captured values.
Check your file content.
You could also replace your == operators by more specific comparisons : String.Compare with case insensitive param, or String.StartsWith() / Contains(), etc, rather than exact comparison.
You could also clean up your input string with "Trim()", etc.
Sorry, but we don't know what is in your file. Maybe problem is in any whitespaces.
In this line of code:
while ((ch = sr.Read()) != '}')
You missed one '='. It should look like that:
while ((ch == sr.Read()) != '}')

Is it possible to do a Levenshtein distance in Excel without having to resort to Macros?

Let me explain.
I have to do some fuzzy matching for a company, so ATM I use a levenshtein distance calculator, and then calculate the percentage of similarity between the two terms. If the terms are more than 80% similar, Fuzzymatch returns "TRUE".
My problem is that I'm on an internship, and leaving soon. The people who will continue doing this do not know how to use excel with macros, and want me to implement what I did as best I can.
So my question is : however inefficient the function may be, is there ANY way to make a standard function in Excel that will calculate what I did before, without resorting to macros ?
Thanks.
If you came about this googling something like
levenshtein distance google sheets
I threw this together, with the code comment from milot-midia on this gist (https://gist.github.com/andrei-m/982927 - code under MIT license)
From Sheets in the header menu, Tools -> Script Editor
Name the project
The name of the function (not the project) will let you use the func
Paste the following code
function Levenshtein(a, b) {
if(a.length == 0) return b.length;
if(b.length == 0) return a.length;
// swap to save some memory O(min(a,b)) instead of O(a)
if(a.length > b.length) {
var tmp = a;
a = b;
b = tmp;
}
var row = [];
// init the row
for(var i = 0; i <= a.length; i++){
row[i] = i;
}
// fill in the rest
for(var i = 1; i <= b.length; i++){
var prev = i;
for(var j = 1; j <= a.length; j++){
var val;
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
row[j - 1] = prev;
prev = val;
}
row[a.length] = prev;
}
return row[a.length];
}
You should be able to run it from a spreadsheet with
=Levenshtein(cell_1,cell_2)
While it can't be done in a single formula for any reasonably-sized strings, you can use formulas alone to compute the Levenshtein Distance between strings using a worksheet.
Here is an example that can handle strings up to 15 characters, it could be easily expanded for more:
https://docs.google.com/spreadsheet/ccc?key=0AkZy12yffb5YdFNybkNJaE5hTG9VYkNpdW5ZOWowSFE&usp=sharing
This isn't practical for anything other than ad-hoc comparisons, but it does do a decent job of showing how the algorithm works.
looking at the previous answers to calculating Levenshtein distance, I think it would be impossible to create it as a formula.
Take a look at the code here
Actually, I think I just found a workaround. I was adding it in the wrong part of the code...
Adding this line
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
so it now reads
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
Seems to fix the problem. Now 'biulding' is 92% accurate and 'bilding' is 88%. (whereas with the original formula 'biulding' was only 75%... despite being closer to the correct spelling of building)

Convert Table to Text with Spaces

I've come across this several times in a couple years of programming so I decided to do some research to see if it was possible. Often I create data structures in code that are initialized in a table like manner, with rows and columns, and I would have liked to have this table-to-text feature for code readability. How can you create a table in word, or excel, or some other program, and output the cells of the table to text, with spaces (not tabs)? Word can do it with tabs, and excel can do it with misaligned spaces. Is there any program out there that automates this?
Have you tried using a monospace font, such as courier, when you export from excel? Most fonts will adjust spacing based on the specific width, height and kerning of each character but a monospace font will allow you to use spaces for alignment.
As for converting tabs to spaces automagically, there must be 100s if not 1000s of methods, apps, commands available out there.
I spent an hour or 2 researching this. I experimented with excel and word and they both came so close to exact solution that it made me crazy. I tried other programs online but with no luck. Here's my solution, Microsoft's Word's Table-To-Text feature and custom C# program that converts the Word-tabified text to column aligned text with spaces and not tabs.
1) Put your columns and rows in an MS Word Table
2) Convert table to text with tabs (look up how to do this)
3) Save the converted table to a plain text file
4) Use my program to open and convert the file
5) Copy the text in the output file to your code
Below is the C# Windows Form Application I wrote. I apologize for lack of optimization. I was at work and wanted it done as quickly as possible:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenFileDialog of = new OpenFileDialog();
of.Title = "Select Tabbed Text File To Convert";
if (of.ShowDialog() != DialogResult.OK)
return;
StreamReader s = new StreamReader(of.OpenFile());
List<string> lines = new List<string>();
string line;
// Get each line into an array of lines.
while ((line = s .ReadLine()) != null)
lines.Add(line);
int numTabs = 0;
// count the number of tabs in each line, assume good input, i.e.
// all lines have equal number of tabs.
foreach (char c in lines[0])
if (c == '\t')
numTabs++;
for (int i = 0; i < numTabs; i++)
{
int tabIndex = 0;
// Loop through each line and find the "deepest" location of
// the first tab.
foreach (string l in lines)
{
int index = 0;
foreach (char c in l)
{
if (c == '\t')
{
if (index > tabIndex)
tabIndex = index;
break;
}
index++;
}
}
// We know where the deepest tab is, now we go through and
// add enough spaces to take the first tab of each line out
// to the deepest.
//foreach (string l in lines)
for (int l = 0; l < lines.Count; l++)
{
int index = 0;
foreach (char c in lines[l])
{
if (c == '\t')
{
int numSpaces = (tabIndex - index) + 1;
string spaces = "";
for (int j = 0; j < numSpaces; j++)
spaces = spaces + " ";
lines[l] = lines[l].Remove(index, 1);
lines[l] = lines[l].Insert(index, spaces);
break;
}
index++;
}
}
}
FileInfo f = new FileInfo(of.FileName);
string outputFile = f.FullName.Insert(f.FullName.IndexOf(f.Extension), " (Aligned)");
StreamWriter w = new StreamWriter(outputFile);
foreach (string l in lines)
w.Write(l + "\r\n");
w.Close();
s.Close();
MessageBox.Show("Created the file: " + outputFile);
}
}
}

Resources