I have a list of installed programs
With Debug.writeLine I can see in the output that there is a discord item.
result.PackageName It can be Discrod, Discord PTB or Discord Canary.
foreach (var installedItem in _installedApps)
{
if (installedItem.DisplayName.Contains("Discord"))
{
Debug.WriteLine(installedItem.DisplayName);
Debug.WriteLine(result.PackageName);
}
if (installedItem.DisplayName.Trim().Contains(result.PackageName.Trim()))
{
isInstalled = true;
}
else
{
isInstalled = false;
}
}
But the code does not work and always returns false
UPDATE:
If I use the following code
var installedStatus = _installedApps.Where(x => x.DisplayName != null && result.PackageName != null && x.DisplayName.Contains(result.PackageName, StringComparison.OrdinalIgnoreCase)).Any();
And in the list of _installedApps, there is only one item named Discord PTB And also in the loop, which is the PackageName Discrod, Discrod PTB or Discrod Canary.
I have a false report that both versions are installed Only PTB should have an installation report.
But if I use the following code
var installedStatus = _installedApps.Where(x => x.DisplayName != null && result.PackageName != null && result.PackageName.Contains(x.DisplayName, StringComparison.OrdinalIgnoreCase)).Select(x => x.Version);
I have the correct report, but the problem is that other programs are not detected
This is not working because isInstalled is getting set by ever iteration.
So the last installedItem can set the value to false.
in essence this is isInstalled is only relevant to the last item.
If you want to use this code just remove the else.
if you want to use linq i would do it like this
isInstalled = _installedApps.Any(i=>i.DisplayName.Trim().Contains(result.PackageName.Trim()))
Related
I wrote a config tool to easily configure msi installers I create with a Visual Studio Setup project. I successfully edited entries in the InstallExecuteSequence table. Now I would like the change something in the Control table as well but the select query returns 0 entries.
using (Database db = new Database(path, DatabaseOpenMode.Transact))
{
using (var vw = db.OpenView(db.Tables["Control"].SqlSelectString))
{
vw.Execute();
Record record = vw.Fetch(); // <= this always returns null
while (record != null)
{
record = vw.Fetch();
if (record == null)
break;
if (record["Dialog_"].ToString().ToLower().Contains("CustomCheckA") && record["Control"].ToString().ToLower().Contains("Text"))
{
tbName.Text = record["Text"].ToString();
}
if (record["Dialog_"].ToString().ToLower().Contains("CustomCheckA") && record["Control"].ToString().ToLower().Contains("BodyText"))
{
tbDescription.Text = record["Text"].ToString();
}
}
if (String.IsNullOrEmpty(eintrag.IDString))
MessageBox.Show("This file does not contain the searched keywords");
vw.Close();
}
db.Close();
}
I believe you need to add more information of the result you want, but I see something here.
if (record["Dialog_"].ToString().ToLower().Contains("CustomCheckA")
You are converting it to lower and then checking if contains that word, but the word is not all lowercase. So the result is always false.
Please check code bellow. I am having wrong entity query i think. check inside second if condition and marked entity query. My goal is save data if this CategoryID not available. But when i run it on debug i can see simply the if condition being skipped however my database not has any data so it should be empty and run if inside codes. I think i am doing something wrong with entity query which marked bellow. Any idea?
ctx.Level1Cats.Any(x => x.Level1CatId != item.CategoryID.ToString())
foreach (CategoryType item in categories)
{
if (item.CategoryLevel == 1)
{
if (ctx.Level1Cats.Any(x => x.Level1CatId != item.CategoryID.ToString()))
{
CatLevel1.Level1CatId = item.CategoryID;
CatLevel1.Name = item.CategoryName;
ctx.Level1Cats.Add(CatLevel1);
//ctx.SaveChanges();
}
else if (ctx.Level1Cats.Any(x => x.Level1CatId == item.CategoryID.ToString()))
{
CatLevel1.Level1CatId = item.CategoryID;
CatLevel1.Name = item.CategoryName;
ctx.Entry(CatLevel1).State = EntityState.Modified;
ctx.SaveChanges();
}
}
}
To solve your problem using your existing code, you can use All inside of the if and then just use a standard else without the additional check:
if (ctx.Level1Cats.All(x => x.Level1CatId != item.CategoryID.ToString()))
{
...
}
else
{
...
}
This will go into your if block if item.CategoryID does not exist (note that All returns true if the list itself is empty). Otherwise, your else will be used instead.
Another option would be to keep the Any but invert the logic:
if (ctx.Level1Cats.Any(x => x.Level1CatId == item.CategoryID.ToString()))
{
// UPDATE.
}
else
{
// ADD.
}
In this case, if any of the items matches the item.CategoryID, you will peform your update. Otherwise, it'll be the Add.
In my experience, the second option shown is more common, where you're looking specifically to find the item of interest and then reacting accordingly.
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 couldn't understand what was happening when I use Linq.Any() method to check if object contains a specific value, the code throws a NullReferenceException on variable with data prior it's use.
The code below:
public ML.Order FetchOrder(ML.MLDBContext db, long OrderID)
{
if (db == null)
db = new ML.MLDBContext();
//avoided code to fetch the Order details from another system via API
Order apiOrder = api.OrdersGet(OrderID);
//avoided code to test null results
bool isNew = false; //to check if fetched order is new or must be updated on DB
//load from DB
ML.Order dbOrder = db.Orders.Where(o => o.OrderID == apiOrder.id).FirstOrDefault();
if (dbOrder == null)
{
isNew = true;
//avoided code to fill dbOrder with apiOrder data
//Below code check if user bought the same product before
//the error is thrown here but it's not null
string ListingID = apiOrder.order_items.First().item.id;
var previousOrders = db.Orders.Where(order => order.OrderID != apiOrder.id && order.CustomerID == apiOrder.buyer.id && order.SellerID == apiOrder.seller.id).ToList();
foreach (ML.Order prevOrder in previousOrders)
{
if (prevOrder.OrderedItems.Any(i => i.ListingID == ListingID)) //Line who causes the error
{
//code to mask as reordered item
}
}
Some points:
I'm sure "apiOrder.order_items.First().item.id" always have any value.
I'm sure the Order contains the item I'm looking for and the field isn't nullable.
When I comment the line who causes the error, the debug will pass through without errors
To solve this problem, I had to replace Linq.Any() by foreach
foreach (ML.Order prevOrder in previousOrders)
{
foreach (ML.OrderedItem item in prevOrder.OrderedItems)
{
if (item.ListingID == ListingID)
{
//code to mask as reordered item
}
}
}
My doubt is:
Does Linq.Any() or EntityFramework Monitor variables prior to it's declaration and use?
Why the NullreferenceException was trowed on variable prior it usage?
What's the problem using the Linq.Any() method to check the existence of a value inside EF object?
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);
}