I hope someone can help with this. For a few years now, I've been iterating through my searchresult using a LOT of If statements to populate all of the properties in my class. There are over 300 properties that I need to populate.. but that is an awful lot of if statements. As an example, this is what I mean:
var myCon = StarfishLDAPCon.CreateDirectoryEntry(objectContainer, whichCM);
var ds = new DirectorySearcher(myCon);
ds.Filter = "(deftyAgentID=07629)";
var result = ds.FindOne();
AgentID returnValue = new AgentID();
if (result.Properties.Contains("deftyAgentID"))
{
returnValue.DeftyAgentID = result.Properties["deftyAgentID"][0].ToString();
}
if (result.Properties.Contains("deftySecuritycode"))
{
returnValue.DeftyAgentID = result.Properties["deftySecuritycode"][0].ToString();
}
if (result.Properties.Contains("deftyAgentACWAgentConsdrdIdle"))
{
returnValue.DeftyAgentACWAgentConsdrdIdle = result.Properties["deftyAgentACWAgentConsdrdIdle"][0].ToString();
}
I have over 300 of those if statements. I'm hoping there's some cool way to do this without all those if statements? Anyone have any ideas?
Thanks for any help!
Dave M.
Well pretty simple - just write a small extension method like this:
public static class SearchResultExtension
{
public string GetPropertyValue(this SearchResult result, string propName)
{
if (result == null || result.Properties == null)
{
return null;
}
if (result.Properties.Contains(propName))
{
return result.Properties[propName][0].ToString();
}
return null;
}
}
Now you can handle the 80% "normal" cases like this:
SearchResult result = ds.FindOne();
AgentID returnValue = new AgentID();
returnValue.DeftyAgentID = result.GetPropertyValue("deftyAgentID");
returnValue.DeftySecurityCode = result.GetPropertyValue("deftySecuritycode");
returnValue.DeftyAgentACWAgentConsdrdIdle = result.GetPropertyValue("deftyAgentACWAgentConsdrdIdle");
and so forth. Any cases that should not return the first element of the Properties collection as a string need to be handled separately, obviously
Related
I am working on MultiSpeak API--I am not familiar with that. A function is as follows:
public meter[] GetMeterByAccountNumber(string accountNumber) {
meter myMeter = new meter();//IS this declaration right?
//some query work and next is sql data reader
int i = 0;
while (rdr.Read())
{
myMeter[i].deviceClass = rdr["deviceClass"].ToString();//error: Cannot apply indexing with [] to type 'meter'
i++;
}
return myMeter[]; //generates ERROR: Value expected.
}
I don't know what this return type of 'GetMeterByAccountNumber' is but it does expect a return of meter[] array.
GetMeterByAccountNumber is not the return type, it's the function name.
You could do somthing like this, however I would call it GetMetersByAccountNumber as it returns an array/IEnumerable
Also i'm not sure what deviceClass has to do with account number..
using System.Linq;
using System.Collections.Generic
public IEnumerable<meter> GetMetersByAccountNumber(string accountNumber) {
var items = new List<meter>();
//some query work and next is sql data reader
while (rdr.Read())
{
var deviceClass = rdr["deviceClass"].ToString();
var meter = new meter();
//Im guessing meter has some properties to set ?
meter.deviceClass = deviceClass;
items.Add(meter);
}
return items.AsReadOnly();
}
Here is what works for me, based on #Richard Friend's Answer. Thanks Richard!
public meter[] GetMetersByAccountNumber(string accountNumber)
{
meter[] final_return;
var items = new List<meter>();
while (rdr.Read())
{
var meter = new meter();
meter.deviceClass = rdr["deviceClass"].ToString();
items.Add(meter);
}
final_return = items.ToArray();
return final_return;
}
In following code snippet I am retrieving notes related to an order. It works fine only if notetext does contain data. Now, while debugging I found that, in other case it throws the exception that Object reference not set to an instance of an object.
I think following snippet looks good, but not sure what is missing, any idea to sort out the problem?
private void fetchDocument(IOrganizationService service, Guid vOrderId)
{
EntityCollection results = null;
string tempNote = string.Empty;
string tempFileName = string.Empty;
ColumnSet cols = new ColumnSet("subject", "filename", "documentbody", "mimetype","notetext");
QueryExpression query = new QueryExpression {
EntityName = "annotation" ,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions = {
new ConditionExpression("objectid",ConditionOperator.Equal,vOrderId)
}
}
};
results = service.RetrieveMultiple(query);
Entity defaultRecord = results.Entities.ElementAtOrDefault(0);
if(defaultRecord.Contains("notetext"))
{
tempNote = defaultRecord.GetAttributeValue<string>("notetext");
}
if (defaultRecord.Contains("filename"))
{
tempFileName = defaultRecord.GetAttributeValue<string>("filename");
}
}
You haven't guarded defaultrecord against null.
results = service.RetrieveMultiple(query);
if (results.Entities == null || !results.Entities.Any()) return;
Entity defaultRecord = results.Entities.ElementAt(0);
Extending the answer to backup result.Entities == null check.
Retrieve multiple EntityCollection is not foolproof.
EntityCollection property:
Decomplied SDK retrieve multiple core:
protected internal virtual EntityCollection RetrieveMultipleCore(QueryBase query)
{
bool? retry = new bool?();
do
{
bool forceClose = false;
try
{
using (new OrganizationServiceContextInitializer(this))
return this.ServiceChannel.Channel.RetrieveMultiple(query);
}
catch (MessageSecurityException ex)
{
..
}
finally
{
this.CloseChannel(forceClose);
}
}
while (retry.HasValue && retry.Value);
return (EntityCollection) null;
}
Decomplied SDK Cached Organization Serivce Context Retrieve multiple:
public override EntityCollection RetrieveMultiple(QueryBase query)
{
RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();
retrieveMultipleRequest.Query = query;
RetrieveMultipleResponse multipleResponse = this.Execute<RetrieveMultipleResponse>((OrganizationRequest) retrieveMultipleRequest);
if (multipleResponse == null)
return (EntityCollection) null;
else
return multipleResponse.EntityCollection;
}
public EntityCollection EntityCollection
{
get
{
if (this.Results.Contains("EntityCollection"))
return (EntityCollection) this.Results["EntityCollection"];
else
return (EntityCollection) null;
}
}
Your issue is actually at this line:
Entity defaultRecord = results.Entities.ElementAtOrDefault(0);
There are no results found, meaning
there is no Annotation that exists with an objectid of "vOrderId", or the user that is performing the query, doesn't have rights to read that record.
Regardless, you should just check for defaultRecord being null or not, and exiting if it is.
This check of null is a common occurrence, which is why I've written this ExtensionMethod:
public Entity GetFirstOrDefault(this IOrganizationService service, QueryBase qb) {
return service.RetrieveMultiple(qb)?.Entities.FirstOrDefault();
}
This would simplify your code to this:
private void fetchDocument(IOrganizationService service, Guid vOrderId)
{
EntityCollection results = null;
string tempNote = string.Empty;
string tempFileName = string.Empty;
ColumnSet cols = new ColumnSet("subject", "filename", "documentbody", "mimetype","notetext");
QueryExpression query = new QueryExpression {
EntityName = "annotation" ,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions = {
new ConditionExpression("objectid",ConditionOperator.Equal,vOrderId)
}
}
};
var defaultRecord = service.GetFirstOrDefault(query);
if(defaultRecord != null)
{
if(defaultRecord.Contains("notetext"))
{
tempNote = defaultRecord.GetAttributeValue<string>("notetext");
}
if (defaultRecord.Contains("filename"))
{
tempFileName = defaultRecord.GetAttributeValue<string>("filename");
}
}
}
I'm using Ncalc in my new project and it already has almost everything I need .
I said almost everything, because now I need to expand some functions and also add new ones such as : nth root,random, etc
Do you know if anyone has already implemented those functions? Or could you give me any tips or guides to extend the function list of Ncalc???
Thanks in advance.
If I understand correctly:
As much as I was using it is by creating a static function
private static void NCalcExtensionFunctions(string name, FunctionArgs functionArgs)
{
if (name == "yourfunctionname")
{
var param1 = functionArgs.Parameters[0].Evaluate();
var param2 = functionArgs.Parameters[1].Evaluate();
//... as many params as you require
functionArgs.Result = (int)param1 * (int)param2; //do your own function logic here
}
if (name == "random")
{
if(functionArgs.Parameters.Count() == 0)
{
functionArgs.Result = new Random().Next();
}
else if(functionArgs.Parameters.Count() == 1)
{
functionArgs.Result = new Random().Next((int)functionArgs.Parameters[0].Evaluate());
}
else
{
functionArgs.Result = new Random().Next((int)functionArgs.Parameters[0].Evaluate(), (int)functionArgs.Parameters[1].Evaluate());
}
}
}
And then you use it as follows
var expr = new Expression("yourfunctionname(3, 2)");
expr.EvaluateFunction += NCalcExtensionFunctions;
var result = expr.Evaluate();
var randExpr = new Expression("random(100)");
randExpr.EvaluateFunction += NCalcExtensionFunctions;
var resultRand = randExpr.Evaluate();
I hope I didn't mistype any of the code. A list of NCalc built-in functions can be found here: http://ncalc.codeplex.com/wikipage?title=functions&referringTitle=Home
I need to do page load on scroll down in my application. I am using couchdb as my back end and I found a pagination option in couchdb which I think would satisfy my issue.
The thing is I can't find any working examples for pagination anywhere. I need someone's help in making my application work with this one.
Take a look at this for reference: https://github.com/soitgoes/LoveSeat/blob/master/LoveSeat/PagingHelper.cs
This is my code. I am getting an error in the options = model.GetOptions(); line, saying "object reference not set to an instance of an object".
public List<newVO> Getdocs(IPageableModel model)
{
List<newVO> resultList = new List<newVO>();
var etag = "";
ViewOptions options = new ViewOptions();
options = model.GetOptions();
options.StartKeyDocId = lastId;
options.Limit = 13;
options.Skip = 1;
var result = oCouchDB.View<newVO>("GetAlldocs", options);
//model.UpdatePaging(options, result);
if (result.StatusCode == HttpStatusCode.NotModified)
{
response.StatusCode = "0";
return null;
}
if (result != null)
{
foreach (newVO newvo in result.Items)
{
resultList.Add(newvo );
}
}
return resultList;
}
Thanks in advance. All ideas are welcome.
public List<newVO> Getdocs(IPageableModel model)
{
List<newVO> resultList = new List<newVO>();
var etag = "";
ViewOptions options = new ViewOptions();
options = model.GetOptions();
options.StartKeyDocId = lastId;
options.Limit = 13;
options.Skip = 1;
var result = oCouchDB.View<newVO>("GetAlldocs", options);
//model.UpdatePaging(options, result);
if (result.StatusCode == HttpStatusCode.NotModified)
{
response.StatusCode = "0";
return null;
}
if (result != null)
{
foreach (newVO newvo in result.Items)
{
resultList.Add(newvo );
}
}
return resultList;
}
This is my code and i am getting error in "options = model.GetOptions();" line that object reference not set to an instance of an object...
I've not used the LoveSeat paging implementation, but you can use the Limit and Skip properties on the ViewOptions to achieve paging:
public static IEnumerable<T> GetPage(this ICouchDatabase couchDatabase,
string viewName,
string designDoc,
int page,
int pageSize)
{
return couchDatabase.View(viewName, new ViewOptions
{
Skip = page * pageSize,
Limit = pageSize
}, designDoc);
}
This simple extension method will get a page of data from a CouchDB view
Is there a way to limit the number of entries WMI retrieves with a WQL statement?
I say this because running a query to retrieve all Win32_NTLogEvent instances is taking forever! All I really need are the most recent events (for about a week, or 2000 entries)
Here's a snippet of the code I'm using to get the log data. Other queries such as Win32_Processor are nice and quick.
if (Configuration.OnlyErrorLogs)
{
// If Information logs should be suppressed, only get events where event type is not 3
WMIDataTemp1 = DataRetriever.GetWMIData("Win32_NTLogEvent", "EventType<>3");
}
else
{
WMIDataTemp1 = DataRetriever.GetWMIData("Win32_NTLogEvent");
}
foreach (ManagementObject Object in WMIDataTemp1)
{
this.Log.Add(new Log(Object));
}
And the functions to get WMI data are as follows:
public static ManagementObject[] GetWMIData(string wmiClass) { return GetWMIData(wmiClass, "", "CIMV2"); }
public static ManagementObject[] GetWMIData(string wmiClass, string whereClause) { return GetWMIData(wmiClass, whereClause, "CIMV2"); }
public static ManagementObject[] GetWMIData(string wmiClass, string whereClause, string nameSpace)
{
try
{
// If a where clause has been set, prepare the clause to add to the query string
if (whereClause != "")
{
whereClause = " WHERE " + whereClause;
}
// Create a search query
string query = "SELECT * FROM " + wmiClass + whereClause;
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher("root\\" + nameSpace, query);
ManagementObjectCollection matches = wmiSearcher.Get();
// Create an array to hold the matches
ManagementObject[] matchArray = new ManagementObject[matches.Count];
// If matches found, copy to output
if(matches.Count > 0)
{
// Copy the search matches into this array
matches.CopyTo(matchArray, 0);
}
// Return array
return matchArray;
}
catch (Exception e)
{
ErrorDialogue errorReporter = new ErrorDialogue(e);
return null;
}
}
Where each Log gets stored:
public class Log
{
public string Category = "N/A";
public string DateTime = "N/A";
public UInt16 ID = 0;
public string Level = "N/A";
public string Message = "N/A";
public string Source = "N/A";
public Log() { }
public Log(ManagementObject wmiLogEvent)
{
this.GetInfo(wmiLogEvent);
}
public void GetInfo(ManagementObject wmiLogEvent)
{
try
{
this.Category = DataRetriever.GetValue(wmiLogEvent, "CategoryString");
this.DateTime = DataRetriever.GetValue(wmiLogEvent, "TimeGenerated");
this.ID = DataRetriever.GetValueUInt16(wmiLogEvent, "EventIdentifier");
this.Level = DataRetriever.ConvertEventType(DataRetriever.GetValueUInt16(wmiLogEvent, "CategoryString"));
this.Message = DataRetriever.GetValue(wmiLogEvent, "Message");
this.Source = DataRetriever.GetValue(wmiLogEvent, "SourceName");
}
catch (Exception e)
{
ErrorDialogue errorReporter = new ErrorDialogue(e);
}
}
}
One option is to use a WHERE clause to specify the range of the entries you want...
For example you could use TimeGenerated in the WHERE clause to specify a time-based range...
Another option is to set BlockSize accordingly when creating ManagementObjectSearcher.
You could use that to specify that you want 2000 entries per call for example - together with an ORDER BY TimeGenerated DESC this should give a nice result.
Speed is not a strong suit for WMI. It tends to be quite memory intensive. However, the question has been addressed and there are a few things you can do. Check out Why are my queries taking such a long time to complete? from Microsoft TechNet.
Now using the System.Diagnostics.EventLog class as a faster alternative. Much more beneficial to the program compared to WMI.
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx