Friday, October 30, 2009
Lambda expression
var mainLinqQuery = from someVar1 in someCollection select someVar1;
some while later after typing more lines of code.
var filteredLinqQuery = mainLinqQuery.Where(someVar2 => someVar2.someProperty == itemToFilterBy);
The result of the filtered LINQ query, filteredLinqQuery will be the specific result that we require.
Friday, October 23, 2009
Converting IList to List
After trolling the intertubes, I found the following, tried it out and what do we know; it works!
Step 1
Declare an empty generic list of whatever type you want.
eg: List
Step 2
Use the AddRange method from the generic list class.
eg: object1.AddRange(IList_object);
Wednesday, October 14, 2009
Left Join revisited.
Click here to navigate to the post
As I recently found out, should you attempt to left join 2 complex objects, you MUST instantiate the object of which will be null (the right sequence, object).
A better way to explain this is to provide an example.
C# code
-------
List
List
var linqQuery = from eList in employeeList
join cList in companyList on
eList.CompanyId equals cList.Id
into joinEmpCompany
from jec in joinEmpCompany.DefaultIfEmpty(new Company())
select new
{
// insert whatever values you want here.
}
As you can see above, I instantiate a new Company object as there could be instances where the join will produce null results for the right-hand side (Company object).
Instantiating the Company object when using the DefaultIfEmpty function will allow LINQ to execute and not produce Null Reference errors.
Thursday, October 8, 2009
Access to Modified Closure error
Franc's blog
I realized the error and would like to point out that LINQ doesn't like empty or null values in the WHERE clause. Be careful unless you like to get a generic LINQ error and go bonkers and waste countless hours scratching your head wondering what went wrong.
245 private void DoNothing(List<SomeObject.SomeClass> someList1, List<SomeObject.RoomRateType> someList2)
246 {
247 string rphValue = String.Empty;
248 List<SomeClass> someClassList = new List<SomeClass>();
249 SomeClass someObjectVar = new SomeClass();
250
251 foreach (SomeObject.SomeClass someObjectVar in someList1)
252 {
253 if (someObjectVar.RPHSpecified)
254 {
255 rphValue = someObjectVar.RPH.ToString();
256 var linqQuery = from rrl in someList2
257 where (rrl.RatePlanCode == rphValue) && (rrl.Rates[0].Base.AdditionalFeesExcludedIndicator == true)
258 select rrl.RoomRateDescription.Name;
259
260 foreach (var linqItem in linqQuery)
261 {
262 someObjectVar.MoreDescription += linqItem;
263 }
264
265 someObjectVar.RphCode = someObjectVar.RPH;
266 }
267 }
268 }
the "rphValue" variable can be null or empty and although I know that what I am doing may not cause any trouble, I am sure in some more complex situation, the results will be dire
Wednesday, October 7, 2009
More on MVC
eg: return Redirect("INSERT_URL_HERE");
MVC allows 1 to use the current http context for validation; As a check on whether the user has logged on or not.
25 if (HttpContext.User.Identity.IsAuthenticated)
26 {
27 //View my previous blog @ http://zacthetechie.blogspot.com/2009/10/mvc-with-web-services.html
28 //on how to access a web service from MVC.
29 return View();
30 }
31 else
32 {
33 return RedirectToAction("LogOn", "Account");
34 }
Thursday, October 1, 2009
MVC with web services
Now, my previous experience with MVC Model is with the conventional SQL database interaction using LINQ to retrieve data.
This time, however, I had to interact with web services. Trolling the web resulted in suggestion such as using a REST architecture structure and others which is quite a good read, especially the post by Rob Conery
Rob Conery - ASPNet MVC : Using Restful Architecture
I decided to experiment with using classes instead and it works. Ok, maybe the guys at the Big Blue will knock me down for this but see below for my explaination
This is what I did.
1. I built a web service library, compiled and NUnit tested thoroughly.
2. In the Model of the MVC Application, I added a "Code File". This resulted in an empty C# file being created.
3. From scratch, I built my Web Service Layer (WSL) class (I wonder if you can just add a class from the "Add") with the required properties to interact with the Web Service library.
1 using System.Collections.Generic;
2 using Your_WebService_ObjectName;
3 using Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses;
4 using Your_WebService_ObjectName.WSObjects.ResLinkAvailibility;
5
6 namespace MVCDemoApplication.Models
7 {
8 public class Your_WebService_Layer_Object
9 {
10 #region Private Fields
11 private Application _app = null;
12 private ResourceTypes _resourceTypes = null;
13 private List<Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses.GetResourceTypeResponse>
14 _getResourceTypeResponseList = null;
15 #endregion
16
17 #region Public Property
18 public ResourceTypes ResourceTypes
19 {
20 get { return _resourceTypes; }
21 set { _resourceTypes = value; }
22 }
23
24 /// <summary>
25 /// A list of GetResourceType response object.
26 /// </summary>
27 public List<Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses.GetResourceTypeResponse> GetResourceTypeResponseList
28 {
29 get { return _getResourceTypeResponseList; }
30 set { _getResourceTypeResponseList = value;}
31 }
32 #endregion
33
34 public Your_WebService_Layer_Object()
35 {
36 _app = new Application("GUID", "PASSWORD", "Another GUID", "<INSERT_YOUR _ URL_HERE>");
37 _resourceTypes = new ResourceTypes(_app);
38 _getResourceTypeResponseList = _resourceTypes.GetResourceType();
39 }
40 }
41 }
4. Added a View that was strongly typed to the class in the Web Service DLL.
And the result generated is as below.