using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_where3 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
where a.Name.Contains("Contoso")
where c.LastName.Contains("Smith")
select new
{
account_name = a.Name,
contact_name = c.LastName
};
foreach (var c in query_where3)
{
System.Console.WriteLine("acct: " +
c.account_name +
"\t\t\t" +
"contact: " +
c.contact_name);
}
}
Use the Distinct Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_distinct = (from c in svcContext.ContactSet
select c.LastName).Distinct();
foreach (var c in query_distinct)
{
System.Console.WriteLine(c);
}
}
Simple Inner Join
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join1 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
select new
{
c.FullName,
c.Address1_City,
a.Name,
a.Address1_Name
};
foreach (var c in query_join1)
{
System.Console.WriteLine("acct: " +
c.Name +
"\t\t\t" +
"contact: " +
c.FullName);
}
}
Self Join
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join5 = from a in svcContext.AccountSet
join a2 in svcContext.AccountSet
on a.ParentAccountId.Id equals a2.AccountId
select new
{
account_name = a.Name,
account_city = a.Address1_City
};
foreach (var c in query_join5)
{
System.Console.WriteLine(c.account_name + " " + c.account_city);
}
}
Double and Multiple Joins
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join4 = from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
join l in svcContext.LeadSet
on a.OriginatingLeadId.Id equals l.LeadId
select new
{
contact_name = c.FullName,
account_name = a.Name,
lead_name = l.FullName
};
foreach (var c in query_join4)
{
System.Console.WriteLine(c.contact_name +
" " +
c.account_name +
" " +
c.lead_name);
}
}
Join Using Entity Fields
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var list_join = (from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
where a.Name == "Contoso Ltd" &&
a.Address1_Name == "Contoso Pharmaceuticals"
select a).ToList();
foreach (var c in list_join)
{
System.Console.WriteLine("Account " + list_join[0].Name
+ " and it's primary contact "
+ list_join[0].PrimaryContactId.Id);
}
}
Late-Binding Left Join
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join8 = from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
into gr
from c_joined in gr.DefaultIfEmpty()
select new
{
contact_name = c_joined.FullName,
account_name = a.Name
};
foreach (var c in query_join8)
{
System.Console.WriteLine(c.contact_name + " " + c.account_name);
}
}
Use the Equals Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_equals1 = from c in svcContext.ContactSet
where c.FirstName.Equals("Colin")
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_equals1)
{
System.Console.WriteLine(c.FirstName +
" " + c.LastName +
" " + c.Address1_City);
}
}
Use the Not Equals Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_ne1 = from c in svcContext.ContactSet
where c.Address1_City != "Redmond"
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_ne1)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
Use a Method-Based LINQ Query with a Where Clause
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var methodResults = svcContext.ContactSet
.Where(a => a.LastName == "Smith");
var methodResults2 = svcContext.ContactSet
.Where(a => a.LastName.StartsWith("Smi"));
Console.WriteLine();
Console.WriteLine("Method query using Lambda expression");
Console.WriteLine("---------------------------------------");
foreach (var a in methodResults)
{
Console.WriteLine("Name: " + a.FirstName + " " + a.LastName);
}
Console.WriteLine("---------------------------------------");
Console.WriteLine("Method query 2 using Lambda expression");
Console.WriteLine("---------------------------------------");
foreach (var a in methodResults2)
{
Console.WriteLine("Name: " + a.Attributes["firstname"] +
" " + a.Attributes["lastname"]);
}
}
Use the Greater Than Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_gt1 = from c in svcContext.ContactSet
where c.Anniversary > new DateTime(2010, 2, 5)
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_gt1)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
Use the Greater Than or Equals and Less Than or Equals Operators
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_gele1 = from c in svcContext.ContactSet
where c.CreditLimit.Value >= 200 &&
c.CreditLimit.Value <= 400
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_gele1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
Use the Contains Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_contains1 = from c in svcContext.ContactSet
where c.Description.Contains("Alpine")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_contains1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
Use the Does Not Contain Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_contains2 = from c in svcContext.ContactSet
where !c.Description.Contains("Coho")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_contains2)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
Use the And and Or Operators
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_andor1 = from c in svcContext.ContactSet
where ((c.Address1_City == "Redmond" ||
c.Address1_City == "Bellevue") &&
(c.CreditLimit.Value != null &&
c.CreditLimit.Value >= 200))
select c;
foreach (var c in query_andor1)
{
System.Console.WriteLine(c.LastName + ", " + c.FirstName + " " +
c.Address1_City + " " + c.CreditLimit.Value);
}
}
Use the OrderBy Operator
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_orderby1 = from c in svcContext.ContactSet
where !c.CreditLimit.Equals(null)
orderby c.CreditLimit descending
select new
{
limit = c.CreditLimit,
first = c.FirstName,
last = c.LastName
};
foreach (var c in query_orderby1)
{
System.Console.WriteLine(c.limit.Value + " " +
c.last + ", " + c.first);
}
}
Use the First and Single Operators
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
Contact firstcontact = svcContext.ContactSet.First();
Contact singlecontact = svcContext.ContactSet.Single(c => c.ContactId == _contactId1);
System.Console.WriteLine(firstcontact.LastName + ", " +
firstcontact.FirstName + " is the first contact");
System.Console.WriteLine("==========================");
System.Console.WriteLine(singlecontact.LastName + ", " +
singlecontact.FirstName + " is the single contact");
}
Retrieving Formatted Values
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var list_retrieve1 = from c in svcContext.ContactSet
where c.ContactId == _contactId1
select new { StatusReason = c.FormattedValues["statuscode"] };
foreach (var c in list_retrieve1)
{
System.Console.WriteLine("Status: " + c.StatusReason);
}
}
Use the Skip and Take Operators without Paging
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_skip = (from c in svcContext.ContactSet
where c.LastName != "Parker"
orderby c.FirstName
select new
{
last = c.LastName,
first = c.FirstName
}).Skip(2).Take(2);
foreach (var c in query_skip)
{
System.Console.WriteLine(c.first + " " + c.last);
}
}
Use a Paging Sort
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_pagingsort1 = (from c in svcContext.ContactSet
where c.LastName != "Parker"
orderby c.LastName ascending,
c.FirstName descending
select new { c.FirstName, c.LastName })
.Skip(2).Take(2);
foreach (var c in query_pagingsort1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
Use .Value to Retrieve the Value of an Attribute
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_value = from c in svcContext.ContactSet
where c.ContactId != _contactId2
select new
{
ContactId = c.ContactId != null ?
c.ContactId.Value : Guid.Empty,
NumberOfChildren = c.NumberOfChildren != null ?
c.NumberOfChildren.Value : default(int),
CreditOnHold = c.CreditOnHold != null ?
c.CreditOnHold.Value : default(bool),
Anniversary = c.Anniversary != null ?
c.Anniversary.Value : default(DateTime)
};
foreach (var c in query_value)
{
System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren +
" " + c.CreditOnHold + " " + c.Anniversary);
}
}
Use the GetAttributeValue Method
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_getattrib = from c in svcContext.ContactSet
where c.GetAttributeValue<Guid>("contactid") != _contactId1
select new
{
ContactId = c.GetAttributeValue<Guid?>("contactid"),
NumberOfChildren = c.GetAttributeValue<int?>("numberofchildren"),
CreditOnHold = c.GetAttributeValue<bool?>("creditonhold"),
Anniversary = c.GetAttributeValue<DateTime?>("anniversary"),
};
foreach (var c in query_getattrib)
{
System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren +
" " + c.CreditOnHold + " " + c.Anniversary);
}
}
Use Math Methods
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_math = from c in svcContext.ContactSet
where c.ContactId != _contactId2
&& c.Address1_Latitude != null &&
c.Address1_Longitude != null
select new
{
Round = Math.Round(c.Address1_Latitude.Value),
Floor = Math.Floor(c.Address1_Latitude.Value),
Ceiling = Math.Ceiling(c.Address1_Latitude.Value),
Abs = Math.Abs(c.Address1_Latitude.Value),
};
foreach (var c in query_math)
{
System.Console.WriteLine(c.Round + " " + c.Floor +
" " + c.Ceiling + " " + c.Abs);
}
}
Use Multiple Select and Where Clauses
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_multiselect = svcContext.IncidentSet
.Where(i => i.IncidentId != _incidentId1)
.Select(i => i.incident_customer_accounts)
.Where(a => a.AccountId != _accountId2)
.Select(a => a.account_primary_contact)
.OrderBy(c => c.FirstName)
.Select(c => c.ContactId);
foreach (var c in query_multiselect)
{
System.Console.WriteLine(c.GetValueOrDefault());
}
}
Use SelectMany
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_selectmany = svcContext.ContactSet
.Where(c => c.ContactId != _contactId2)
.SelectMany(c => c.account_primary_contact)
.OrderBy(a => a.Name);
foreach (var c in query_selectmany)
{
System.Console.WriteLine(c.AccountId + " " + c.Name);
}
}
Use String Operations
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_string = from c in svcContext.ContactSet
where c.ContactId == _contactId2
select new
{
IndexOf = c.FirstName.IndexOf("contact"),
Insert = c.FirstName.Insert(1, "Insert"),
Remove = c.FirstName.Remove(1, 1),
Substring = c.FirstName.Substring(1, 1),
ToUpper = c.FirstName.ToUpper(),
ToLower = c.FirstName.ToLower(),
TrimStart = c.FirstName.TrimStart(),
TrimEnd = c.FirstName.TrimEnd(),
};
foreach (var c in query_string)
{
System.Console.WriteLine(c.IndexOf + "\n" + c.Insert + "\n" +
c.Remove + "\n" + c.Substring + "\n"
+ c.ToUpper + "\n" + c.ToLower +
"\n" + c.TrimStart + " " + c.TrimEnd);
}
}