(List<Fruit>)Session["listSession"]
the session list is created in my home page.
and i would like to access information on another page
I would like to loop throw
if ((List<Fruit>)Session["listSession"].name == "apple ")
{
item.(access a method in my fruit class)
}else {
// do something else
}
\
List<Fruit> fruits = Session["listSession"] as List<Fruit>;
if(fruits != null)
{
foreach(Fruit fruit in fruits)
{
if(fruit.name=="apple")
fruit.Method();
else
//do something else
}
}
A couple of points here: you can just grab the instance from the session as a list and keep a reference to it, then you can check it is something (not null) and that it contains something which is also something (if nullable), before grabbing a reference of that and performing desired actions:
var fruitList = Session["listSession"] as List<Fruit>;
if (fruitList != null && fruitList.Count > 0)
{
var fruit = fruitList[0];
if (fruit != null && fruit.name == "apple ")
{
fruit.Consume();
}
}
That ought to help, though I'm sure you'll need to build on it to further your purpose.
Related
I would like to ask for your help. I want to validate my delete request class. The rule is:
"Deletion allowed only for one element with the biggest price in
database for each type of product"
. repositoryEntities is not null but the Result is: Enumeration yielded no results.
Can you please check below code and let me know what is wrong?
private void CanIDelete(DeleteProductRequest entity, CustomContext context)
{
var repositoryEntities = _productRepository.Queryable.Where(s => s.TypeOfProductId.Equals(entity.TypeOfProductId) && !s.IsDeleted);
if (repositoryEntities != null)
{
var theBiggestPriceInDatabase = Decimal.MinValue;
foreach (var repositoryEntity in repositoryEntities)
{
if (theBiggestPriceInDatabase < repositoryEntity.Price)
{
theBiggestPriceInDatabase = repositoryEntity.Price;
continue;
}
if (entity.Price != theBiggestPriceInDatabase)
{
context.AddFailure("Deletion allowed only for one product 'with the biggest price' in database for each type of product");
return;
}
}
}
Here i have example of getting inventory from json string.
inventory = JsonUtility.FromJson<InventoryModel>GFile.GetPlayer(FileComponent.PlayerInventory));
Since i am loading that string from file it is possible it is just blank and i want to first check if it is blank and i would do it like this:
if(GFile.GetPlayer(FileComponent.PlayerInventory) != " ")
{
inventory = JsonUtility.FromJson<InventoryModel>(GFile.GetPlayer(FileComponent.PlayerInventory));
}
So my question is if there is any more elegant way of doing this instead of typing if statement like this?
Why not like this? :
var player = GFile.GetPlayer(FileComponent.PlayerInventory);
if(!string.IsNullOrWhiteSpace(player)) {
inventory = JsonUtility.FromJson<InventoryModel>(player);
}
I'd suggest
string data = GFile.GetPlayer(FileComponent.PlayerInventory);
if(!string.IsNullOrWhiteSpace(data))
{
inventory = JsonUtility.FromJson<InventoryModel>(data);
}
This way you only call GetPlayer once, and it doesn't matter if the resulting data is an empty string or is full of whitespace - it still won't enter that block and set inventory.
Edit
For older versions of .Net, this will also work
string data = GFile.GetPlayer(FileComponent.PlayerInventory);
if(data != null && data.Trim().Length == 0)
{
inventory = JsonUtility.FromJson<InventoryModel>(data);
}
foreach (Objecta a in aList())
{
foreach (Objectb b in bList)
{
if (a.variable != b.variable1 && a.variable() != b.variable2)
{
a.setVariable("Error");
}
}
}
The problem I am getting is that it goes through the foreach loop the first time and it sets variable to error without checking if other values (when it goes through the loop again) finds a match.
What I would like is to wait until it goes through all the lists and at the last foreach loop iteration if nothing in aList matches the variable target && variable source in bList then finally set it to Error flag.
Any suggestions to get around this will be appreciated.
Try doing it the other way around. Search for a match instead of searching for non-matches.
foreach (Objecta a in aList())
{
bool foundMatch = false;
foreach (Objectb b in bList)
{
if (a.variable == b.variable1 || a.variable() == b.variable2)
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
a.setVariable("Error");
}
}
I think this is what you are looking for. So if StoreList is the outer loop and LinkList is the inner loop. You want to search all the links to see if there's an ID that matches the store ID. If you find a match, stop searching the links. After the search through the links, set an error on the store if there was no match, then go to the next store.
foreach (Objecta a in aList())
{
var foundMatch = false;
foreach (Objectb b in bList)
{
if (a.variable == b.variable1 || a.variable() == b.variable2)
{
fondMatch = true;
break;
}
}
if (!foundMatch) a.setVariable("Error");
}
I think you want something like this:
First select all the item values from aList and bList and put them in a seperate array:
var aVals = aList.Select(x=>x.value1).ToArray();
var bListVals1 = bItems.Select(x=>x.value1).ToArray();
var bListVals2 = bItems.Select(x=>x.value2).ToArray();
var bVals = bListVals1.Concat(bListVals2);
Then, get the values both lists have in common:
var correctVals = bVals.Intersect(aVals);
These are the correct values and so all the other values are wrong:
var wrongVals = aVals.Except(correctVals);
Now you have the values that are wrong and can act accordingly:
wrongAItems = aList.Where(a => wrongVals.Contains(a.value));
foreach(wrongA in wrongAItems){
wrongA.setVariable("Error");
}
foreach (Store s in processFlowStores.getStoresList())
{
if (!processFlowLinks.Any(l => s.getNodeId() == l.getLinkSource() ||
s.getNodeId() == l.getLinkTarget()))
{
s.setID("Error: FailedOperation Error - 123.123.121");
}
}
EDIT: more compact solution using Linq. Basically, if none of the links has it as either source or target, mark it as error.
I have a user object that contains a nested list and i need to change the value of a element in the 3rd list and return the user object.
I want to do this with linq, below is the nested loop.
foreach (User itm in user)
{
if (itm.destinations!=null)
{
foreach (Models.Api.destinations.Destination dm in itm.destinations)
{
if (dm.destinationData != null)
{
foreach (Models.Api.destinations.DestinationData destData in dm.destinationData)
{
if (destData.type == "phone" & destData.data!="")
{
//i want to update the destData.data here .. something like
destData.data ='updated data';
}
}
}
}
}
}
I want the updated data to be available in the user object
can someone help me achieve this with the LINQ
Thanks in advance
Tarak
Try this:
foreach (var x in
(from itm in user
where itm.destinations!=null
from dm in itm.destinations
where dm.destinationData != null
from destData in dm.destinationData
where destData.type == "phone" & destData.data != ""
select new { itm, dm, destData }))
{
/* put your update code here. */
}
You didn't give us what the update code should look like or even the object models for us to work from.
I am checking to see if items already match whats in my MSSQL DB. I am using LINQ to update records. I would like to know how i can check if an item is equal to d_0_2 or if its equal to null/empty. How would i go about doing this?
below is my existing code, which partially works. but is failing due to the null/Empty
if (updateProduct.studioId == Convert.ToInt32(d_0_2.SelectedValue)) { }
else { updateProduct.studioId = Convert.ToInt32(d_0_2.SelectedValue);}
Thanks in advance.
I'm not sure if I understood you question correctly, but you want to check if item is null or if not is it studioId equal to d_0_2.SelectedValue
if (updateProduct == null)
{
//create new update product
}
else if (updateProduct.studioId != Convert.ToInt32(d_0_2.SelectedValue))
{
updateProduct.studioId = Convert.ToInt32(d_0_2.SelectedValue);
}
string value = d_0_2.SelectedValue.ToString();
// what if SelectedValue is empty or null?
if (!string.IsNullOrEmpty(value))
return;
// what if product is null?
if (updateProduct != null)
return;
if (updateProduct.studioId != null &&
updateProduct.studioId == Convert.ToInt32(value))
{
// you have product and its id equal to d_0_2.SelectedValue
}
else
{
// studioId not equal to to d_0_2.SelectedValue
updateProduct.studioId = Convert.ToInt32(value);
}