How to get nested records from your object class List using linq.

Hello All,

Today we are going to explore on “how to get nested records from your object class List using linq.”

Here we will take a very small example of Category and SubCategory. We will first create 2 classes namely Category and SubCategory.

public class Category
{
            public int CategoryID { get; set; }
            public string CategoryName { get; set; }
            public List SubCategoryList { get; set; }
            public bool IsCategorychecked { get; set; }
}

public class SubCategory
{
            public int SubCategoryID { get; set; }
            public string SubCategoryName { get; set; }
            public bool IsSubCategorychecked { get; set; }
}

Lets fill the List object of Category.

List<Category> categoryList = new List<Category>();

Category category = new Category();

category.CategoryID = 1;

category.CategoryName = “Category One”;

SubCategory subCategory = new SubCategory();

subCategory.SubCategoryID = 1;

subCategory.SubCategoryName = “Sub Category One”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 2;

subCategory.SubCategoryName = “Sub Category Two”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 3;

subCategory.SubCategoryName = “Sub Category Three”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = true;

categoryList.Add(category);

category = new Category();

category.CategoryID = 2;

category.CategoryName = “Category Two”;

SubCategory subCategory = new SubCategory();

subCategory.SubCategoryID = 4;

subCategory.SubCategoryName = “Sub Category Four”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 5;

subCategory.SubCategoryName = “Sub Category Five”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 6;

subCategory.SubCategoryName = “Sub Category Six”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = false;

categoryList.Add(category);

category = new Category();

category.CategoryID = 3;

category.CategoryName = “Category Three”;

subCategory = new SubCategory();

subCategory.SubCategoryID = 7;

subCategory.SubCategoryName = “Sub Category Seven”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 8;

subCategory.SubCategoryName = “Sub Category Eight”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 9;

subCategory.SubCategoryName = “Sub Category Nine”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = true;

categoryList.Add(category);

Now if you want to get the selected Sub Category List for a particular category name so you can do the following query:

var query = (from item in _category
                   from nested in item.SubCategory
                   where nested.IsSubCategoryChecked.Equals(true)
                   select nested.SubCategoryName);

The above query will give result as:

Sub Category Two

Sub Category Three

Sub Category Seven

Sub Category Eight

Happy Coding!!!! :)

Exceptions while Configuring WCF on Windows 7

Hello All,

Recently I have installed Windows 7 on my machine and was trying to configure all my existing projects. While configuring my Service Project on IIS, I was getting some exceptions. Finally I could manage to configure it. If you are getting exception which says:

HTTP Error 404.3 - Not Found.

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Or

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

To resolve this you just need to run “%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i”
 This will create svc mapping.

Also if anyone is getting Forbidden exceptions, like this:

HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory.

Go to IIS, select the Virtual Directory on the left side. Once you select the Virtual directory on right side of that window you will find an option of Directory Browsing. Double click on it and check whether it’s enabled or disable in the Actions List. Click on enable in Action List.

I didn’t not check it on Vista but if you are getting same exceptions then you could try with the same settings. Hope it will work on Vista also.


Please leave your comments to help me improve my blog.

Thank You for visiting the Blog. Happy Coding! :)

Lock wait timeout exceeded; try restarting transaction in MySQL

Today while I was trying to clean up the MySQL Database, I got the following error:
Error Code : 1205
Lock wait timeout exceeded; try restarting transaction
(0 ms taken)

I was surprise to see this error as the same query was working fine before few minutes and then what happen all of sudden. The error came because of some other guy was trying to delete records simultaneously without my knowledge and so the transaction got locked. After doing some Google search I could figure out the solution for it.

Here is what you have to do:

Just run “SHOW INNODB STATUS” in your query browser you will get the status of INNODB which contains LIST OF TRANSACTIONS FOR EACH SESSION. In that just find all the Active thread id’s, say you find one thread id = 148 and then just select and kill that thread id, like this: KILL 148.


That’s all you have to do.

Hope that helps.


Please leave your comments to help me improve my blog.
 
Thank You for visiting the Blog. Happy Coding! :)

How to find Max/Min ID in DataTable

These days I'm working on both ASP.NET and Silvelight. Though I'm working on ASP.NET after a year now, thought posting some of the articles on ASP.NET which I had in my mind but couldnt devote much time. Here is one of them.
How to find the Max/Min ID in DataTable. DataTable has Select Method by which you can get the related records as you do it in SQL or for any database. I'm creating an Employee DataTable which has EmployeeID, FirstName and Lastname columns and fill that with some dummy data.


DataTable objDT = new DataTable();
DataColumn objDC = new DataColumn("EmployeeID", System.Type.GetType("System.Int32"));
objDT.Columns.Add(objDC);
objDC = new DataColumn("FirstName", System.Type.GetType("System.String"));objDT.Columns.Add(objDC);
objDC = new DataColumn("LastName", System.Type.GetType("System.String"));

objDT.Columns.Add(objDC);

DataRow objDR = objDT.NewRow();
objDR["EmployeeID"] = 1;
objDR["FirstName"] = "Lalit";
objDR["LastName"] = "Sharma";
objDT.Rows.Add(objDR);

 
objDR = objDT.NewRow();
objDR["EmployeeID"] = 2;
objDR["FirstName"] = "Aditi";
objDR["LastName"] = "Deodhar";
objDT.Rows.Add(objDR);

objDR = objDT.NewRow();
objDR["EmployeeID"] = 3;
objDR["FirstName"] = "Nimish";
objDR["LastName"] = "Navadkar";
objDT.Rows.Add(objDR);

objDR = objDT.NewRow();
objDR["EmployeeID"] = 4;
objDR["FirstName"] = "Pragati";
objDR["LastName"] = "Dukale";
objDT.Rows.Add(objDR);

objDR = objDT.NewRow();
objDR["EmployeeID"] = 5;
objDR["FirstName"] = "Rumpa";
objDR["LastName"] = "Gune";
objDT.Rows.Add(objDR);

objDR = objDT.NewRow();
objDR["EmployeeID"] = 6;
objDR["FirstName"] = "Abhijit";
objDR["LastName"] = "Parkhi";
objDT.Rows.Add(objDR);

//This is the select query for Max
string selectCommand = "Max(EmployeeID)";

//returns the Max EmploeeID
int nextID = Convert.ToInt32((object)objDT.Compute(selectCommand, string.Empty));
Likewise we can find Min ID in DataTable.
 
Hope that helps.
 
Please leave your comments to help me improve my blog.

Thankyou for visiting the Blog. Happy Coding! :)