Search Results

Search found 10 results on 1 pages for 'khnle'.

Page 1/1 | 1 

  • Building an external list while filtering in LINQ

    - by Khnle
    I have an array of input strings that contains either email addresses or account names in the form of domain\account. I would like to build a List of string that contains only email addresses. If an element in the input array is of the form domain\account, I will perform a lookup in the dictionary. If the key is found in the dictionary, that value is the email address. If not found, that won't get added to the result list. The code below will makes the above description clear: private bool where(string input, Dictionary<string, string> dict) { if (input.Contains("@")) { return true; } else { try { string value = dict[input]; return true; } catch (KeyNotFoundException) { return false; } } } private string select(string input, Dictionary<string, string> dict) { if (input.Contains("@")) { return input; } else { try { string value = dict[input]; return value; } catch (KeyNotFoundException) { return null; } } } public void run() { Dictionary<string, string> dict = new Dictionary<string, string>() { { "gmail\\nameless", "[email protected]"} }; string[] s = { "[email protected]", "gmail\\nameless", "gmail\\unknown" }; var q = s.Where(p => where(p, dict)).Select(p => select(p, dict)); List<string> resultList = q.ToList<string>(); } While the above code works (hope I don't have any typo here), there are 2 problems that I do not like with the above: The code in where() and select() seems to be redundant/repeating. It takes 2 passes. The second pass converts from the query expression to List. So I would like to add to the List resultList directly in the where() method. It seems like I should be able to do so. Here's the code: private bool where(string input, Dictionary<string, string> dict, List<string> resultList) { if (input.Contains("@")) { resultList.Add(input); //note the difference from above return true; } else { try { string value = dict[input]; resultList.Add(value); //note the difference from above return true; } catch (KeyNotFoundException) { return false; } } } The my LINQ expression can be nicely in 1 single statement: List<string> resultList = new List<string>(); s.Where(p => where(p, dict, resultList)); Or var q = s.Where(p => where(p, dict, resultList)); //do nothing with q afterward Which seems like perfect and legal C# LINQ. The result: sometime it works and sometime it doesn't. So why doesn't my code work reliably and how can I make it do so?

    Read the article

  • Is it OK to open a DB4o file for query, insert, update multiple times?

    - by Khnle
    This is the way I am thinking of using DB4o. When I need to query, I would open the file, read and close: using (IObjectContainer db = Db4oFactory.OpenFile(Db4oFactory.NewConfiguration(), YapFileName)) { try { List<Pilot> pilots = db.Query<Pilot>().ToList<Pilot>(); } finally { try { db.Close(); } catch (Exception) { }; } } At some later time, when I need to insert, then using (IObjectContainer db = Db4oFactory.OpenFile(Db4oFactory.NewConfiguration(), YapFileName)) { try { Pilot pilot1 = new Pilot("Michael Schumacher", 100); db.Store(pilot1); } finally { try { db.Close(); } catch (Exception) { }; } } In this way, I thought I will keep the file more tidy by only having it open when needed, and have it closed most of the time. But I keep getting InvalidCastException Unable to cast object of type 'Db4objects.Db4o.Reflect.Generic.GenericObject' to type 'Pilot' What's the correct way to use DB4o?

    Read the article

  • How to prevent ADO.NET from altering double values when it reads from Excel files

    - by Khnle
    I have the following rows in my Excel input file: Column1 Column2 0-5 3.040 6 2.957 7 2.876 and the following code which uses ADO.NET to read it: string fileName = "input.xls"; var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName); var dbConnection = new OleDbConnection(connectionString); dbConnection.Open(); try { var dbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", dbConnection); var dbReader = dbCommand.ExecuteReader (); while (dbReader.Read()) { string col1 = dbReader.GetValue(0).ToString(); string col2 = dbReader.GetValue(1).ToString(); } } finally { dbConnection.Close(); } The results are very disturbing. Here's why: The values of each column in the first time through the loop: col1 is empty (blank) col2 is 3.04016411633586 Second time: col1 is 6 col2 is 2.95722928448829 Third time: col1 is 7 col2 is 2.8763272933077 The first problem happens with col1 in the first iteration. I expect 0-5. The second problem happens at every iteration with col2 where ADO.NET obviously alters the values as it reads them. How to stop this mal-behavior?

    Read the article

  • What's a clean way to have the server return a JavaScript function which would then be invoked?

    - by Khnle
    My application is architected as a host of plug-ins that have not yet been written. There's a long reason for this, but with each new year, the business logic will be different and we don't know what it will be like (Think of TurboTax if that helps). The plug-ins consist of both server and client components. The server components deals with business logic and persisting the data into database tables which will be created at a later time as well. The JavaScript manipulates the DOM for the browsers to render afterward. Each plugin lives in a separate assembly, so that they won't disturb the main application, i.e., we don't want to recompile the main application. Long story short, I am looking for a way to return JavaScript functions to the client from an Ajax get request, and execute these JavaScript functions (which are just returned). Invoking a function in Javascript is easy. The hard part is how to organize or structure so that I won't have to deal with maintenance problem. So concat using StringBuilder to end up with JavaScript code as a result of calling toString() from the string builder object is out of the question. I want to have no difference between writing JavaScript codes normally and writing Javascript codes for this dynamic purpose. An alternative is to manipulate the DOM on the server side, but I doubt that it would be as elegantly as using jQuery on the client side. I am open for a C# library that supports chainable calls like jQuery that also manipulates the DOM too. Do you have any idea or is it too much to ask or are you too confused? Edit1: The point is to avoid recompiling, hence the plug-ins architecture. In some other parts of the program, I already use the concept of dynamically loading Javascript files. That works fine. What I am looking here is somewhere in the middle of the program when an Ajax request is sent to the server. Edit 2: To illustrate my question: Normally, you would see the following code. An Ajax request is sent to the server, a JSON result is returned to the client which then uses jQuery to manipulate the DOM (creating tag and adding to the container in this case). $.ajax({ type: 'get', url: someUrl, data: {'': ''}, success: function(data) { var ul = $('<ul>').appendTo(container); var decoded = $.parseJSON(data); $.each(decoded, function(i, e) { var li = $('<li>').text(e.FIELD1 + ',' + e.FIELD2 + ',' + e.FIELD3); ul.append(li); }); } }); The above is extremely simple. But next year, what the server returns is totally different and how the data to be rendered would also be different. In a way, this is what I want: var container = $('#some-existing-element-on-the-page'); $.ajax({ type: 'get', url: someUrl, data: {'': ''}, success: function(data) { var decoded = $.parseJSON(data); var fx = decoded.fx; var data = decode.data; //fx is the dynamic function that create the DOM from the data and append to the existing container fx(container, data); } }); I don't need to know, at this time what data would be like, but in the future I will, and I can then write fx accordingly.

    Read the article

  • Selecting a form which is in an iframe using jQuery

    - by Khnle
    I have a form inside an iframe which is inside a jQuery UI dialog box. The form contains a file input type. The jQuery UI dialog contains an Upload button. When this button is clicked, I would like to programmatically call the submit method. My question is how I could select the form which is in a iframe using jQuery. The following code should clarify the picture: <div id="upload_file_picker_dlg" title="Upload file"> <iframe id="upload_file_iframe" src="/frame_src_url" frameborder=0 width=100% scrolling=no></iframe> </div> frame_src_url contains: <form action="/UploadTaxTable" enctype="multipart/form-data" method="post" id="upload-form"> <p>Select a file to be uploaded:</p> <p> <input type="file" name="datafile" size="60"> </p> The jQueryUI dialog box javascript code: $('#upload_file_picker_dlg').dialog({ ... buttons: { 'Close': function() { $(this).dialog('close'); }, 'Upload': function() { $('#upload-form').submit(); //question is related to this line $(this).dialog('close'); } }, .... }); From the javascript code snippet above, how can I select the form with id upload-form that is in the iframe whose id is upload_file_iframe ?

    Read the article

  • What is an alternative to ADO.NET, but elegant way in C# to read Excel files

    - by Khnle
    ADO.NET just fails miserably when it's used to read Excel files where a column contains mixed data types. It appears that it tries to determine the data type of a column, and then assume the rest of the entire column is of the same data type. Here are some backgrounds: http://stackoverflow.com/questions/2968111/how-to-prevent-ado-net-from-altering-double-values-when-it-reads-from-excel-files http://blog.lab49.com/archives/196 What is an alternative approach that doesn't require automation, Excel to be co-installed, is simple (since the input excel file will only have one worksheet in each file).

    Read the article

  • Are there any well-known algorithms or computer models that computer scientists use to predict FIFA

    - by Khnle
    Occasionally I read news articles that mention about some computer models that computer scientists use to predict winners of some sporting events or the odds for betting which I think there must be a mathematical model behind it. I never bothered to think twice even though I am a "pseudo computer scientist" myself. With the 2010 FIFA World Cup just underway, and since I am also a "pseudo football/soccer player" myself, I just started to wonder about these calculations algorithms. For example, I know one factor is determining the strength of opponents, so that a win against a strong opponent can count more than a win against a weak opponent. But it now kind of gets in a circular loop, or at least how does one determine the strength of a team in the first place, before that team can be considered strong or weak? If it's based on a historical data then there's no way that could be accurate, because those players of the past are no longer on the fields so their impact is none (except maybe if they become coaches like Maradona) Anyway, long question short, if you're happen to be working in this field or have some knowledge, please shed some lights.

    Read the article

  • .NET: Calling GetInterface method of Assembly obj with a generic interface argument

    - by Khnle
    I have the following interface: public interface PluginInterface<T> where T : MyData { List<T> GetTableData(); } In a separate assembly, I have a class that implements this interface. In fact, all classes that implement this interface are in separate assemblies. The reason is to architect my app as a plugin host, where plugin can be done in the future as long as they implement the above interface and the assembly DLLs are copied to the appropriate folder. My app discovers the plugins by first loading the assembly and performs the following: List<PluginInterface<MyData>> Plugins = new List<PluginInterface<MyData>>(); string FileName = ...;//name of the DLL file that contains classes that implement the interface Assembly Asm = Assembly.LoadFile(Filename); foreach (Type AsmType in Asm.GetTypes()) { //Type type = AsmType.GetInterface("PluginInterface", true); // Type type = AsmType.GetInterface("PluginInterface<T>", true); if (type != null) { PluginInterface<MyData> Plugin = (PluginInterface<MyData>)Activator.CreateInstance(AsmType); Plugins.Add(Plugin); } } The trouble is because neither line where I am getting the type as by doing Type type = ... seems to work, as both seems to be null. I have the feeling that the generic somehow contributes to the trouble. Do you know why?

    Read the article

  • Define a method in base class that returns the name of itself (using reflection) - subclasses inheri

    - by Khnle
    In C#, using reflection, is it possible to define method in the base class that returns its own name (in the form of a string) and have subclasses inherit this behavior in a polymorphic way? For example: public class Base { public string getClassName() { //using reflection, but I don't want to have to type the word "Base" here. //in other words, DO NOT WANT get { return typeof(Base).FullName; } return className; //which is the string "Base" } } public class Subclass : Base { //inherits getClassName(), do not want to override } Subclass subclass = new Subclass(); string className = subclass.getClassName(); //className should be assigned "Subclass"

    Read the article

1