Is there built-in standard method to act like If functional? [closed] - c#

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 days ago.
Improve this question
I currently work on project to adapt several design patterns that I think, it pleasant in eyes to maintain. the one is nested less. In c++ I can achieve it; there's a standard method that can do it.
public IActionResult OnPostAsync()
{
if (ModelState.IsValid)
{
// do something validation again with persistence context
var query = context.Users
.Where(u => Username == this.Username &&
u => Password == this.Password)
.FirstOrDefault<User>();
// double nested again
if (query is not null)
{
return RedirectToPagePermanent("/dashboard");
}
//
}
return Page();
}
What I expected is the code will like this before I implemented it myself.
bool state = ModelState.IsValid;
null result = FooFunction(expected return type, pass context function like ContextFunction(username, password));
return FooFunction(IActionResult, RedirectToPagePermanent("/dashboard");
So FooFunction is standard common to do encapsulate situation like this.
In C++, this kind function does exist, so I think C# will have it, too.

Related

Collection of Types rather than Instances [closed]

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 2 years ago.
Improve this question
I have a list of viewmodels that currently instantiates all the viewmodels at the start:
private ObservableCollection<ViewModelWithTemplate> StepViewModels
{
get
{
if (_stepViewModels == null)
_stepViewModels = new ObservableCollection<ViewModelWithTemplate>
{
new AddRemoveCellsViewModel(TemplateRows),
new ActivateDeactivateCellsViewModel(TemplateRows),
new RotateCellsViewModel(TemplateRows),
new SetAcquisitionOrderViewModel(TemplateRows),
new NameTemplateViewModel(TemplateRows)
};
return _stepViewModels;
}
}
public ViewModelWithTemplate CurrentStepViewModel => StepViewModels[StepIndex];
The problem is that the VMs are created all at once, but I want them to be only created when needed, mainly so that they have the most recent edition of TemplateRows.
How can I do this? My first thought is to have the collection be a list of types so that I can instantiate each viewmodel when I arrive at it, like this:
public ViewModelWithTemplate CurrentStepViewModel => new StepViewModels[StepIndex](TemplateRows);
But a little Googling around makes me think the answer is more toward inversion of control, and/or a VM factory. I'm having trouble finding actual helpful information that's not tied to some framework. (Prism, etc)
You can use Func delegates (assuming StepIndex is of type int and TemplateRows is of type TemplateRowsType):
// this can be moved to static prop:
var stepViewModelsCtorDict = new Dictionary<int, Func<TemplateRowsType, ViewModelWithTemplate>>
{
{0, trows => new AddRemoveCellsViewModel(trows) },
{1, trows => new ActivateDeactivateCellsViewModel(trows) },
....
};
var result = stepViewModelsCtorDict[StepIndex](TemplateRows);

How to write in the equivalent but terse code, maybe using by Linq? [closed]

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 5 years ago.
Improve this question
How to write in the equivalent but terse code, maybe using by Linq??
This code:
var sectionAttributePairs = new List<SectionAttributePair>();
where SectionAttributePair is my own class
entityAttributes is AttributeMetadata[] type
List<AttributeMetadata> attributesBeyoundSection = entityAttributes.ToList();
foreach (var pair in sectionAttributePairs)
{
foreach (var attr in entityAttributes.ToList())
{
if (pair.Attribute.Contains(attr.LogicalName))
{
attributesBeyoundSection.Remove(attr);
}
}
}
Is attributesBeyoundSection a List<T>? Then you can use List.RemoveAll
var matchingAttributes = entityAttributes
.Where(attr => sectionAttributePairs
.Any(pair => pair.Attribute.Contains(attr.LogicalName)));
attributesBeyoundSection.RemoveAll(matchingAttributes.Contains);
Note that the class must either override Equals and GetHashCode or implement IEquatable<T>(best is both always). Otherwise Contains would just compare references.

I have a method which returns class type in c# [closed]

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'm new to programming and C# and i have this below question which i do not know what to call it as
I have a method as
public Account GetAccountDetails(){ //where Account is a class
return new Account{
text = GetNameOfAccount;
length = GetLengthOfAccountNumber;
};
}//end of method
I knew that i would be able to get the properties and fields from the above method and i can use it. But, what do you call the above method as some object initializers or anonymous type? How is it useful than explicitly defining an object for Account class and using the object for accessing the methods?
I think the method you mentioned is like a ordering the Set Menu of McDonald.
Imagine if you are working in McDonald and large number of people stand in front of you.
If Set Menu is not exist, every people order buger and coke and potato or other menu separately. It test your brain.
but in real world, some people order Set menu and some people order separately.
so you can memory them more efficiently.
It is also express to code.
public Order getSetA() {
return new Order {
Buger = Menu.CheeseBugger;
Drink = Menu.Coke;
SideMenu = Menu.Potato;
};
}
public Order getSetB() {
return new Order {
Buger = Menu.BigMac;
Drink = Menu.Water;
SideMenu = Menu.Pickle;
};
}
Order order = new Order();
RandomBugerForToday(out order.Buger);
RandomDrinkForToday(out order.Drink);
order.SideMenu = null;

How do I stub IQueryable<int> and IQueryable<String> [closed]

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 7 years ago.
Improve this question
How do I stub these methods:
ProductDAL:
public IQueryable<string> getCatNameList()
{
var db = new Database();
var categoryNames = db.ProductCategories.Select(c => c.Name);
return categoryNames;
}
public IQueryable<int> getCatIdList()
{
var db = new Database();
var categoryID = db.ProductCategories.Select(c => c.Categoryid);
return categoryID;
}
IProductDAL:
IQueryable<int> getCatIdList();
IQueryable<string> getCatNameList();
Been searching on stackoverflow, but no questions/answers that gets me any closer to understanding how I go about doing this.
In this case I would recommend you change your interface to a series of IEnumerable rather than IQueryable. There is little point having these methods as queryables as they have already been projected.
If you are just trying to stub them, use Enumerable.Empty<type>().AsQueryable().
Create and IEnumerable of whatever you need to return (can be a List or an array). Then convert it to IQueryable using AsQueryable extension method and pass it to whatever mocking/stubbing library you use.

Pull content from tablen into a class [closed]

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 7 years ago.
Improve this question
I sit in a problem that do that it will not udhente some content from the database into my class,
it must be such that I have to download content from the database.
error are
Provides for a specific object reference to the 'point.db' which is a field, a method or a property that is not static
i have try to this here
DataClassesDataContext db = new DataClassesDataContext();
public static string OpretBrugerPointAdd()
{
PointBonus pointbonusStart = db.PointBonus.FirstOrDefault(P => P.Id == 1);
if (pointbonusStart != null)
{
return Convert.ToString(pointbonusStart.point);
}
}
when I call it happens like this,
string pointAntal = point.OpretBrugerPointAdd();
You're trying to use non-static field from static method. That's not going to work.
You should instantiate new DataClassesDataContext within your method instead:
public static string OpretBrugerPointAdd()
{
using(var db = new DataClassesDataContext())
{
PointBonus pointbonusStart = db.PointBonus.FirstOrDefault(P => P.Id == 1);
if (pointbonusStart != null)
{
return Convert.ToString(pointbonusStart.point);
}
}
}
You also have to deal with a fact, that your method doesn't return anything when if statement condition evaluates to false. You should probably add return null (or other default value you wish) at the very end of you mehtod to fix that.

Categories