Friday, 25 April 2014

using First, FirstOrDefault, DefaultIfEmpty, Single, SingleOrDefault

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay", "Maya" };
int[] number = { 1, 5, 6, 8, 20, 15, 34, 67, 98, 12, 23 };
 var num = number.Where(n => n > 100).FirstOrDefault(); //num=0
 var numm = number.Where(n => n > 100).First(); //will give an error
 var nummm = number.Where(n => n > 100).DefaultIfEmpty(1).First(); //nummm=1
 var nam = names.Where(n => n.StartsWith("z")).FirstOrDefault();//nam=null
 var name = names.Where(n => n.StartsWith("M")).SingleOrDefault();
//will give an error because two values found with “M”

Share:

Find Customer list whom city exists in city array

String[] _cities = { "London", "New Delhi" };

var _customers = new[] { new { Name = "Saurabh", City = "London" },
 new { Name = "Sumit", City = "New Delhi" },
 new { Name = "Rohit", City = "London" },
 new { Name = "Rahul", City = "Mumbai" },
 new { Name = "Suresh", City = "London" },
 new { Name = "Sunil", City = "New Delhi" },
 new { Name = "Mahesh", City = "Agra" }
 };

var _query = from c in _customers
 where (_cities).Contains(c.City)
 select c;
Share:

Tuesday, 25 September 2012

Select text, value, remove, selecteIndex on select element

1. Add options to the end of a select

$("#drpCountry").append('');

$("#drpCountry").append('');

or

$('').appendTo($("#drpCountry"));

$('').appendTo($("#drpCountry"));

2. Add options to the start of a select

$("#drpCountry").prepend('');

3. Replace all the options with new options

$("#drpCountry").html('');

4. Replace items at a certain index

$("#drpCountry option:eq(1)").replaceWith('');

$("#drpCountry option:eq(2)").replaceWith('');

5. Set the element at index 2 to be selected


$("#drpCountry option:eq(2)").attr("selected", "selected");

6. Set the selected element by text

$("#drpCountry").val("India").attr("selected", "selected");

7. Set the selected element by value

$("#drpCountry").val("2");

8. Remove an item at a particular index

$("#drpCountry option:eq(0)").remove();

9. Remove first item

$("#drpCountry option:first").remove();

10. Remove last item

$("#drpCountry option:last").remove();

11. Get the text of the selected item

var selectedtext=$("#drpCountry option:selected").text();

12. Get the value of the selected item

var selectedvalue=$("#drpCountry option:selected").val();

13. Get the index of the selected item

var selectedIndex=$("#drpCountry option").index($("#drpCountry option:selected"));

14. Alternative way to get the selected item

var selectedItem=$("#drpCountry option:selected").prevAll().size();

15. Insert an item in after a particular position

$("#drpCountry option:eq(0)").after("");

16. Insert an item in before a particular position

$("#drpCountry option:eq(3)").before("");

17. Getting values when item is selected

$("#drpCountry").change(function() {

var Selectedvalue=$(this).val();

var Selectedtext=$(this).children("option:selected").text();

});
Share: