Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I got a enum like this:
public enum PlatForms
{
AAA=1,
BBB=2,
CCC=3
}
and a function like this:
public List<Something> GetSomething(PlatForms pf)
{
switch(pf)
{
case PlatForms.AAA:
var some = context.table1.Where(x => x.Prop == true);
break;
case PlatForms.BBB:
var some2 = context.table2.Where(x => x.Prop == true);
break;
default:
break;
}
//do convert;
//
}
The difference is table name is not same. How do I rewrite the code to make it simple?
Would something like this work?
You wouldn't want this Dictionary code to run every single time, so as soon as you have your context you could initialize a Dictionary...
var tableTranslation = new Dictionary<Platforms, Context>();
tableTranslation.Add(Platforms.AAA, context.table1);
tableTranslation.Add(Platforms.BBB, context.table2);
Then in your GetSomething() method you could just reference the Dictionary's table.
var some = tableTranslation[pf].Where(x => x.Prop == true);
//do convert
Would that do what you're looking for?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to do a function such that when the user selects the class name and clicks the button, it create a new List<SelectedClass>. Currently I am using a switch statement like so:
switch(Name)
case"a" : List<a> X=new List<a>();
case"b" : List<b> X=new List<b>();
Is there another method to do this?
Try it with Activator.CreateInstance (so you won't need any switch statement):
Type type = Type.GetType(Name); // Example: Type.GetType("NamespaceName.ClassName")
Type listType = typeof(List<>);
var listWithType = listType.MakeGenericType(new [] { type });
var instanceOfList = Activator.CreateInstance(listWithType);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Recently I watched Pluralsight course "Functional programming in C#". And rewrote some methods in my hobby project. Now they look like this:
public override CommandLineResult GetResult()
{
return Disposable.Using(new IndicatorRepository(), repo =>
{
return repo.AddOrUpdateIndicator(Indicator, Value, DateTimeExtensions.LocalNow().Date);
})
.Map((p) => new NumericIndicatorRecorderedModel()
{
Id = Guid.NewGuid().ToString(),
DbActionPreformed = true,
IsRewritten = Convert.ToBoolean(p),
IndicatorName = Indicator,
Value = Value,
ValueDate = ValueDate
})
.Map((model) => new CommandLineResult()
{
ActionName = "~/Views/CommandLine/_clresult_NumericIndicatorRecorderedModel.cshtml",
Model = model
});
}
There's something pretty about this chaining approach (I removed Using with Disposable class, at every step I have expressions, not statements, etc.).
But the questions are:
Does not this approach covers some dangers? and what are they? (isn't it more difficult to debug for example)
What usually people do in terms of chaining approach, if it is required to branch the flow of code execution? Break chain with ifs somewhere? Write "map-if" functional extensions (would not that approach create spaghetti code?)?
UPDATE: more specific question regarding this issue
What approach to implement code branching is better - to break chaining like this:
public string GetResult(bool param) {
if (param) {
return MyClass.DoOneWay(p =>...).AlsoDo(q =>...).ToString();
}
else
{
return MyClass.DoOtherWay(p =>...).AlsoDo(q =>...).ToString();
}
}
Or to implement functional if-helpers, like this:
public string GetResult(bool param)
{
return MyClass.DoIf(param, p => ...., q => ....).AlsoDo(q =>...).ToString();
}
And what to do, if there're much more options then just true/false?
How to "functionaly" implement "switch" branching?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to update EF record, in method I have EF object and another new objct which I want to use to update data from. But I m not sure how to copy data from new object to existing one.
Help please.
Here is my code:
public int PostHomeLead(string _lead)
{
try
{
int result = 0;
Lead lead = new Lead();
lead = new JavaScriptSerializer().Deserialize<Lead>(_lead);
//check if lead exist with same session id, if so update it other wise add new.
Lead existingLead = new Lead();
existingLead = db2.HomeLoanCustRepo.GetByID(lead.Lead_id);
if (existingLead == null)
{
db2.HomeLoanCustRepo.Insert(lead);
db2.Save();
result = 1;
}
else
{
db2.HomeLoanCustRepo.Update(lead);
db2.Save();
result = 1;
}
return result;
}
catch(Exception ex)
{
throw ex;
}
}
Either map the properties manually:
existingLead.Foo = deserializedLead.Foo;
existingLead.Bar = deserializedLead.Bar;
existingLead.Baz = deserializedLead.Baz;
Or use a library that does this, like AutoMapper.
As for your comment, creating a deep copy is what you seem to be after. Note this allows for overposting or mass assignment when you don't verify which properties may be updated. You'll need to Attach() the cloned object when using cloning or mapping, as it will not be the same object as returned by GetByID(), so Entity Framework's change tracker won't recognize it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a c# method that can be reused multiple times,but depending on the condition of the variable type, i would like to be able to construct the name of a text field.
For example, if type="TY" then I would like to call
if ((String)this.TYIdlabelChange.Value == null)
However, if type="CA", then I would like to call
if ((String)this.CAIdlabelChange.Value == null)
Other examples:
if ((String)this.DIIdlabelChange.Value == null)
if ((String)this.LOIdlabelChange.Value == null)
if ((String)this.REIdlabelChange.Value == null)
etc...
Any ideas?
Thank you!
You need use Page.FindControl, something like this
var tb = FindControl(type+"IdlabelChange") as Textbox;
if(tb != null && tb.Value != null){
....
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an object named 'Account' and the following code:
Account objAcc = new Account();
objAcc.Name = "Test";
The above is working fine, as expected. I now have a list of values as follows:
string props = "Name,Address,Telephone";
Now, what I want to do is see if "Name" exists in that list. I only have the object to use though (hard coding a case statement etc isn't possible as the object is dynamic), so from objAcc.Name, I somehow need to get "Name" from that, and then see if it's in the list.
Thanks in advance, I hope it's clear enough,
Dave
You can use reflection, by doing that :
var properties = objAcc.GetType().GetProperties();
foreach(var property in properties)
{
if(props.Contains(property.Name))
{
//Do you stuff
}
}
string test = objAcc.GetType().GetProperty("Name") == null ? "" : objAcc.GetType().GetProperty("Name").Name;
bool result = "Name,Address,Telephone".Split(',').Contains(test);
You may use the following method if you like:
public bool PropertyExists<T>(string propertyName, IEnumerable<string> propertyList,T obj)
{
string test = obj.GetType().GetProperty(propertyName) == null ? "" : obj.GetType().GetProperty(propertyName).Name;
bool result = propertyList.Contains(test);
return result;
}
Giannis