Projections and DBNull (Left Joins)

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 25-Nov-2011 21:15:44   

Using ORMSupportClasses v2.6.09.0305 vs. SQL Server.

So I am investigating using projections for the first time, mainly as a replacement for datasets/dynamic lists in cases where I want a strongly typed custom object.

I currently have some code that works fine as a dynamic list, and wanted to see if it's possible to do easily with projections. As an example, say I have a Order table that has a personId column on it. The personId represents the sales rep that placed the order, which can be null if the order was placed without a sales rep's help.

Currently I fetch a Datatable and then manually create an anonymous type. Something like the following (note that this is a made-up example and the code may not be exactly right):



            var list = new List<object>();
            foreach (DataRow row in dataTable.Rows)
            {
                var userName = string.Empty;
                if (row[PersonFields.FirstName.Name] != DBNull.Value && row[PersonFields.LastName.Name] != DBNull.Value)
                {
                    personName = row[PersonFields.FirstName.Name] + " " +  row[PersonFields.LastName.Name];
                }

                list.Add(new
                             {
                                 UserName = personName
                              });
            }


So basically I am checking to see if the person table returned null in the left join; if it's null, I show string.empty, and if it's non-null, I show the values.

Is it possible (and recommended) to do this kind of thing with a projection?

Thanks,

Phil

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Nov-2011 00:34:01   

Hi Phil,

That code looks ok. You also could project to a custom class. In that custom class you hava a nullable PersonId, then you can show the VendorName property based on the nullability of the other field. You also can project to entities. You also could do this at projection-time. For instance you can create a subclass of the projector. Example: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=8997

David Elizondo | LLBLGen Support Team