Why is this dynamic list comparison failing? - c#

I have a search that returns a result that is dynamic. So I am trying to just show a label if there are no results found. The problem i am having is i dont know how to count the result because it is dynamic and is not equal to a type.
The error message is :
Operator '!=' Cannot be applied ot operands of type
System.Collections.Generic.List and int
if (Page.IsValid)
{
string keyword = txtSearch.Text.Trim();
List<dynamic> results = SearchItems(keyword);
List<dynamic> Cresults = SearchContacts(keyword);
if(results != 0 || Cresults !=0)
{
//bind and return
LVI.DataSource = results;
LVI.DataBind();
// System.Threading.Thread.Sleep(500);
//Contact Bind return
LVC.DataSource = Cresults;
LVC.DataBind();
// System.Threading.Thread.Sleep(250);
lvAdmin.DataSource = results;
lvAdmin.DataBind();
LVCAdmin.DataSource = Cresults;
LVCAdmin.DataBind();
}
else{
NoResults.Visible = true;
}

You cannot just do:
if(results != 0 || Cresults !=0)
{
}
That way your comparing the actual List to 0, which obviously fails.
Just do:
if(results.Count != 0 || Cresults.Count !=0)
{
}
Or:
if(results.Any() || Cresults.Any())
{
}

Use the Count property of the List class:
if (results.Count != 0 || Cresults.Count != 0)
{
//rest of code
}
Docs: http://msdn.microsoft.com/en-us/library/a7f69ad7.aspx

you can try using count
if(results.Count > 0 || Cresults.Count > 0)

Related

Search with multiple conditions?

I am currently working on a search for a website and I am stuck with a problem, which doesn't even seem that hard to solve. I'm just not able to figure the solution out myself.
Situation: In the current state of my search I can sort the events to dates (I can select October for instance and it will only show the events which happen during october), for categories (The events each have categories) and even search text. Now when I select october as a month and festival as a category, I get all events occuring in october AND all festivals. I want only the festivals which occur in october. Any ideas on how I could achieve this?
This is what I've got so far:
var validEvents = new List<Item>();
var allEvents = ((LinkField)this.controlItem.Fields["Event Container"]).TargetItem.Children.ToList(); // getting the events
if (this.ddlMonths.SelectedIndex != 0 || this.ddlCategories.SelectedIndex != 0 || this.searchQuery.Text != string.Empty)
{
foreach (var currentEvent in allEvents)
{
var isValid = false;
// 1. Check for months
if (this.ddlMonths.SelectedIndex != 0)
{
// .. validation if event should be displayed
if (startDate <= monthDates[1].Date && endDate >= monthDates[0].Date)
{
isValid = true;
}
}
// 2. Check for categories
if (this.ddlCategories.SelectedIndex != 0)
{
foreach (var eventCategory in ((MultilistField) currentEvent.Fields["Categories"]).GetItems())
{
if (eventCategory["Category"].ToLower() == this.ddlCategories.SelectedValue.ToLower())
{
isValid = true;
}
}
}
// 3. Check for search query
if (this.searchQuery.Text != string.Empty)
{
var searchText = this.searchQuery.Text;
if (currentEvent["Title"].Contains(searchText) || currentEvent["Text"].Contains(searchText))
{
isValid = true;
}
}
if (isValid)
{
validEvents.Add(currentEvent);
}
}
}
Using LINQ you can combine search conditions as follows
var allEvents = ...;
IEnumerable<Item> events = allEvents;
if (ddlMonths.SelectedIndex != 0)
events = events.Where(condition1);
if (ddlCategories.SelectedIndex != 0)
events = events.Where(condition2);
if (searchQuery.Text != string.Empty)
events = events.Where(condition3);
var validEvents = events.ToList();
I would put
var isValid = true;
in line 8, then if any of the conditions is NOT met set it to false. And also skip any following check if isValid is already false.
Hope it helps!
EDIT - Complete code:
var validEvents = new List<Item>();
var allEvents = ((LinkField)this.controlItem.Fields["Event Container"]).TargetItem.Children.ToList(); // getting the events
if (this.ddlMonths.SelectedIndex != 0 || this.ddlCategories.SelectedIndex != 0 || this.searchQuery.Text != string.Empty)
{
foreach (var currentEvent in allEvents)
{
var isValid = true;
// 1. Check for months
if (this.ddlMonths.SelectedIndex != 0)
{
// .. validation if event should be displayed
if (!(isValid && startDate <= monthDates[1].Date && endDate >= monthDates[0].Date))
{
isValid = false;
}
}
// 2. Check for categories
if (this.ddlCategories.SelectedIndex != 0)
{
foreach (var eventCategory in ((MultilistField) currentEvent.Fields["Categories"]).GetItems())
{
if (!(isValid && eventCategory["Category"].ToLower() == this.ddlCategories.SelectedValue.ToLower()))
{
isValid = false;
}
}
}
// 3. Check for search query
if (this.searchQuery.Text != string.Empty)
{
var searchText = this.searchQuery.Text;
if (!(isValid && currentEvent["Title"].Contains(searchText) || currentEvent["Text"].Contains(searchText)))
{
isValid = true;
}
}
if (isValid)
{
validEvents.Add(currentEvent);
}
}
}

Multithread, Linq to Sql, ConcurrentDictonary Fails Remove

I have a ConcurrentDictionary of Attributes for products. These attributes have the product ID and values for the names of the attribute and any options the attribute has. I have this ConcurrentDictionary because I have threads that are created to handle each attribute in the dictionary by attribute name.
if (knownAttribute.AttributeType.Value.Equals("Product Specification"))
{
Console.WriteLine("Started a thread for: " + knownAttribute.AttributeTypeId + ", " + knownAttribute.Value);
while (true)
{
/* if (AS400SpecificationAttributes.IsEmpty && knownSpecificationBag.IsEmpty && gatherRowsTasks.All(x => x.IsCompleted))
break;*/
AS400SpecificationAttribute AS400SpecificationAttributeWork = null;
AS400SpecificationAttributeWork = knownSpecificationBag.Keys.FirstOrDefault(x => x.AttributeName == knownAttribute.Value);
if (AS400SpecificationAttributeWork != null)
{
var product = ctx.Products.FirstOrDefault(x => x.ProductNumber == AS400SpecificationAttributeWork.ProductNumber);
if (product == null)
continue;
var productAttribute = new ProductAttribute();
productAttribute.Attribute = knownAttribute;
if (AS400SpecificationAttributeWork.AttributeValue != null)
{
var knownAttributeOption = ctx.AttributeOptions.FirstOrDefault(x => x.Attribute.Equals(knownAttribute) && x.Value.Equals(AS400SpecificationAttributeWork.AttributeValue));
if (knownAttributeOption == null)
{
knownAttributeOption = new AttributeOption();
knownAttributeOption.Value = AS400SpecificationAttributeWork.AttributeValue;
knownAttributeOption.Attribute = knownAttribute;
ctx.AttributeOptions.InsertOnSubmit(knownAttributeOption);
ctx.SubmitChanges();
}
productAttribute.AttributeOption = knownAttributeOption;
productAttribute.AttributeOptionId = knownAttributeOption.Id;
}
product.ProductAttributes.Add(productAttribute);
ctx.SubmitChanges();
string tmpstr = null;
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
Thread.Sleep(50);
}
else
{
if (tryCounter < 5)
{
tryCounter++;
Thread.Sleep(1000);
Console.WriteLine("Thread waiting for work: Product Specification:" + knownAttribute.Value);
continue;
}
else
{
int outVal;
threadTracker.TryRemove("Product Specification:" + knownAttribute.Value, out outVal);
Console.WriteLine("Closing Thread: Product Specification:" + knownAttribute.Value);
break;
}
}
Thread.Sleep(50);
}
It seems like the following Attribute element refuses to be removed.
I don't understand why. If i put it in a while(!dic.tryRemove(ele)) it will forever be stuck and never move from there.
There may be an error somewhere within the thread but I have no idea why.
This statement
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
will always return true or false. It won't block. That's the behavior of ConcurrentDictionary. It will return false if the key is not in the dictionary.
If you're looping while that method returns false and it's stuck, that means that the item isn't in the dictionary when the loop begins. Either it either was never in the dictionary or that another thread already removed it.
Is your intention to loop until the item is not in the dictionary?
You could try this:
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr)
&& !knownSpecificationBag.ContainsKey(AS400SpecificationAttributeWork))
Implement proper equals and gethashcode when using TryRemove
public override int GetHashCode()
{
return new { this.name, this.value, this.group, this.productNumber }.GetHashCode();
}
public bool Equals(AS400SpecificationAttribute other)
{
if (other == null)
return false;
return (this.ProductNumber.Equals(other.productNumber) && ((this.group != null && this.group.Equals(other.AttributeGroup)) || (this.group == null && other.AttributeGroup == null)) && ((this.name!= null && this.name.Equals(other.AttributeName)) || (this.name == null && other.AttributeName == null)) && ((this.value != null && this.value.ToUpper().Equals(other.AttributeValue.ToUpper())) || (this.value == null && other.AttributeValue == null)));
}

How to convert this COUNT to INT?

string query = "SELECT COUNT(DISTINCT [PIN]) AS [pinCount] FROM [Users]";
DataTable dt = Execute(query);
if (dt == null || dt.Rows.Count == 0 || dt.Rows[0][0] == DBNull.Value)
return false;
else if ((int)dt.Rows[0]["pinCount"] > 0)
return true;
return false;
i have this kind of query in my code, and when casting the count to int, it gives me an exception that when castin the value must be less then infinite...what am i doing wrong?
Try this code
if (dt != null)
{
if (dt.Rows.Count > 0)
{
object tVal = dt.Rows[0][0];
if (!Convert.IsDBNull(tVal))
return (Convert.ToInt32(tVal) > 0);
}
}
return false;

Iterate through specific col in DGVs' & compare the string values

I got 2 datagrid views, First datagrid 1st col or Index 0 & the Second datagrid 1st col or Index 0.
How can i loop through a specific col in the datagridviews and look for the string values,if it matches with the list then go to a function.
My approach is not working. How can i do this?
private void b_calculate_Click(object sender, EventArgs e)
{
List<string> value = new List<String>() { "AE0", "AT1", "AT2", "AT3"};
value = new List<string>(datagridview1.Columns[0].Index);
List<string> value2 = new List<String>() { "BE0", "BT1", "BT2", "BT3"};
value2 = new List<string>(datagridview2.Columns[0].Index);
//First Combination
if((value.ToString() == "AT1" || value.ToString() == "AE0" ||
value.ToString() == "AT2")
&&
(value2.ToString() == "BT1" || value2.ToString() == "BE0"))
{
gottoFunction1();
}
//Second Combination
if((value.ToString() == "AT1" || value.ToString() == "AT2" )
&&
(value2.ToString() == "BT1" || value2.ToString() == "BT2"))
{
gottoFunction2();
}
}
Here's a routine that iterates through all available rows in both DataGridViews and does the processing shown in your method:
private void b_calculate_Click(object sender, EventArgs e)
{
for (var i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView2.RowCount <= i)
break;
var cellFromDG1 = dataGridView1.Rows[i].Cells[0];
var cellFromDG2 = dataGridView1.Rows[i].Cells[0];
if (cellFromDG1.Value == null || cellFromDG2.Value == null)
{
// this could be the empty row that allows you to
// enter a new record
continue;
}
var value = cellFromDG1.Value.ToString();
var value2 = cellFromDG2.Value.ToString();
if ((value == "AT1" || value == "AE0" || value == "AT2") &&
(value2 == "BT1" || value2 == "BE0"))
{
gottoFunction1();
}
if ((value == "AT1" || value == "AT2") &&
(value2 == "BT1" || value2 == "BT2"))
{
gottoFunction2();
}
}
}

Sorting List of Strings

I'm having a List<String> l_lstTemp and which contains
"A1"
"A1_1"
"A1_2"
"1A"
"B2_1"
"B1_2"
"B1_1_2"
"A10"
"B11"
"A"
"Z"
I need to sort the items based on the character and numeric value.
So the sorted list will be like
"1A"
"A"
"A1"
"A1_1"
"A1_2"
"A10"
"B1_1_2"
"B1_2"
"B2_1"
"B11"
"Z"
Here is my code:
l_lstTemp.Sort(delegate(String One, String Two)
{
Match l_mOne = Regex.Match(One, #"(\D*)(\d*)");
Match l_mTwo = Regex.Match(Two, #"(\D*)(\d*)");
int Result;
if (l_mOne.Success || l_mTwo.Success)
{
String l_strX, l_strY;
l_strX = l_mOne.Groups[1].Value;
l_strY = l_mTwo.Groups[1].Value;
Result = l_strX.CompareTo(l_strY);
if (Result != 0)
return Result;
l_strX = l_mOne.Groups[2].Value;
l_strY = l_mTwo.Groups[2].Value;
if (l_strX == String.Empty || l_strY == String.Empty)
{
Result = l_strX.CompareTo(l_strY);
if (Result != 0)
return Result;
}
else
{
long X = long.Parse(l_strX);
long Y = long.Parse(l_strY);
Result = X.CompareTo(Y);
if (Result != 0)
return Result;
}
}
return 0 ;
}
);
But its not working (sorting) properly.
How do I modify my code to sort the list properly?
Please post me a way to do this.
Thanks in advance.
I did some modifications to your code. The thing was that when both Group 1 and Group 2 are equals, you still need to check what remains.
Important: I did the modifications inside your code, so this could be a little tricky. I really suggest you refactoring your code now that you know it works:
l.Sort(delegate(String One, String Two)
{
while (One != "" && Two != "")
{
if (One == Two)
return 0;
//Add one more group to capture what remains of the expression
Match l_mOne = Regex.Match(One, #"_*(\D*)(\d*)(.*)$");
Match l_mTwo = Regex.Match(Two, #"_*(\D*)(\d*)(.*)$");
int Result;
if (l_mOne.Success || l_mTwo.Success)
{
String l_strX, l_strY;
l_strX = l_mOne.Groups[1].Value;
l_strY = l_mTwo.Groups[1].Value;
Result = l_strX.CompareTo(l_strY);
if (Result != 0)
return Result;
l_strX = l_mOne.Groups[2].Value;
l_strY = l_mTwo.Groups[2].Value;
if (l_strX == String.Empty || l_strY == String.Empty)
{
Result = l_strX.CompareTo(l_strY);
if (Result != 0)
return Result;
}
else
{
long X = long.Parse(l_strX);
long Y = long.Parse(l_strY);
Result = X.CompareTo(Y);
if (Result != 0)
return Result;
One = l_mOne.Groups[3].Value; //Store in 'One' the remaining part of the regex
Two = l_mTwo.Groups[3].Value; //The same in Two
continue; //The result will be the result of the comparison of those two new values.
}
}
}
return One.CompareTo(Two);
});
Edit:
I also added _* to remove all the _ characters from the begining of the strings. I assumed here that the strings will only contain _ after the numbers and not something like B1B or B1$.
The thing here is that you don't really explain how the comparison should be made, and I had to assume those things from your original data and the sorted data, otherwise what would happen if you want to sort A1A and A1_? What should it return?
Here's how I would implement such a comparer. Much easier to follow IMHO.
var re = new Regex(#"^(\d+)?([A-Z]+)(\d+)?(?:_(\d+)(?:_(\d+))?)?$");
Func<Group, int, int> intOrDefault = (g, d) => g.Success ? Convert.ToInt32(g.Value) : d;
list.Sort((x, y) =>
{
var xm = re.Match(x);
var ym = re.Match(y);
int cmp;
// compare the first group
// compare the leading numbers (if any)
cmp = intOrDefault(xm.Groups[1], int.MaxValue).CompareTo(intOrDefault(ym.Groups[1], int.MaxValue));
if (cmp != 0)
return cmp;
// compare letters
cmp = xm.Groups[2].Value.CompareTo(ym.Groups[2].Value);
if (cmp != 0)
return cmp;
// compare the trailing numbers (if any)
cmp = intOrDefault(xm.Groups[3], 0).CompareTo(intOrDefault(ym.Groups[3], 0));
if (cmp != 0)
return cmp;
// compare the next group
cmp = intOrDefault(xm.Groups[4], 0).CompareTo(intOrDefault(ym.Groups[4], 0));
if (cmp != 0)
return cmp;
// compare the last group
cmp = intOrDefault(xm.Groups[5], 0).CompareTo(intOrDefault(ym.Groups[5], 0));
return cmp;
});
For this example, just calling sort l_lstTemp.Sort() would get you the result you are looking for

Categories

Resources