Linq if null select something else code example

Example 1: linq select if different null

using (MyTestDBEntities context = new MyTestDBEntities())
            {
                var query = context.UserTables.Where(p => p.City != null)
                    .Select(cc => new { DisplayValue = cc.UserID, DisplayText = cc.FirstName + "  " + cc.LastName + "  " + cc.City });

                DropDownList1.DataSource = query.ToList();
                DropDownList1.DataTextField = "DisplayText";
                DropDownList1.DataValueField = "DisplayValue";
                DropDownList1.DataBind();
            }

Example 2: linq select if different null

db.UserAdresses.Where(p => p.UserID == guid && p.PostalCode != null)

Tags:

Misc Example