Recently I ran into an issue with the contains method in LINQ in conjunction with the SingleOrDefault method.
My query was similar to:
var data = (from i in ctx.DataContext.orders
where order.Contains(i.Name)
select i).SingleOrDefault();
and this was throwing the error:
System.InvalidOperationException: Sequence contains more than one element
Turns out the Contains translated to a LIKE '%...%' and was returning multiple records. By changing this to i.Name == order, then I got back the single result I was expecting.
Recently I had the need to handle the selected index changed event in an ASP.NET page. I did not want to post back to the server as I just wanted to hide/show an HTML element, so jQuery was the perfect fit.
Below is the code. Basically, within the jQuery document ready funciton, I wire a function to each dropdown list ('select') change event. You will probably want to refine this to a single id or class. Each time the dropdown selected index changes, I want to check the text value, and if it contains some text, either show or hide an HTML element. I also set a checkbox control to checked using the client id within a script tag.
<script type="text/javascript">
$(function() {
$('select').change(function(e) {
if (this[this.selectedIndex].text.indexOf("SomeText") > 0) { $('#emailActivity').fadeIn("slow");
// Ensure checkbox initially checked
$('#<%=cbSendEmail.ClientID %>').attr('checked', true);
}
else {$('#emailActivity').fadeOut();
}
});
});
</script>