Linq query on EntityCollection

linq

Querying SQL database all the time may affect performance so some time we can pull the generic data and then query on that result collection may give better result.

Here is the example of it,

  • Query database and get entity collection 
 /// <summary>
 /// get Entity Collection
 /// </summary>
 /// <param name="QuoteData"></param>
 /// <returns></returns>
 private EntityCollection GetQuoteLines(Entity QuoteData)
 {
 
 QueryExpression query = new QueryExpression("quotedetail");
 query.ColumnSet = new ColumnSet(true);

 query.Criteria.AddCondition("quoteid", ConditionOperator.Equal, QuoteData.Attributes["quoteid"].ToString());

 query.AddOrder("soft_section", OrderType.Ascending);
 query.AddOrder("soft_categorygroup", OrderType.Ascending);

 return service.RetrieveMultiple(query);
 }
  • fire a linq query on that EntityCollection 
    • get single record 
  • /// <summary>
     /// early binding
     /// </summary>
     /// <param name="criteriaValue"></param>
     /// <param name="theEntityCollection"></param>
     /// <returns></returns>
     private IEnumerable<Entity> GetEntity(Guid criteriaValue, EntityCollection theEntityCollection)
     {
     IEnumerable<Entity> entity = from ent in theEntityCollection.Entities.AsEnumerable()
     where ent.Id == criteriaValue
     select ent;
     return (IEnumerable<Entity>)entity;
     }
    
     /// <summary>
     /// Late binding
     /// </summary>
     /// <param name="criteriaValue"></param>
     /// <param name="theEntityCollection"></param>
     /// <returns></returns>
     private IEnumerable<Entity> GetEntity(string criteriaValue, EntityCollection theEntityCollection)
     {
     IEnumerable<Entity> entity = from ent in theEntityCollection.Entities.AsEnumerable()
     where ent["Id"] == criteriaValue
     select ent;
     return (IEnumerable<Entity>)entity;
     }
    • get multiple records 
//get sections from L-collection with fileter section N line Q-type
 var quotedetailSections = (from c in QuoteLinesCollection.Entities
 where ((c.Contains("soft_section") && (string)(c["soft_section"]) == FilterSection)
 && (c.Contains("soft_lineqtype") && (c.FormattedValues["soft_linetype"]) == FilterQlineType))
 select new
 {
 lineQType = c.Contains("soft_linequotetype") ? c.FormattedValues["hsb_linequotetype"] : string.Empty,
 amount = c.Contains("extendedamount") ? ((Money)c["extendedamount"]).Value : 0,
 cat = c.Contains("soft_categorygroup") ? (string)c["soft_categorygroup"] : string.Empty,
 sec = c.Contains("soft_section") ? c["soft_section"].ToString() : string.Empty

 }).ToList();

 

thanks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s