Monday, May 20, 2013

Linq to Sharepoint with working on Entities

Filling upcoming grid Data.

1. Specifing Pager object with rowlist 10 in the upcoming grid Data
2. create entity class for DeliverableItem and assigning search filds value see below
  deliverableFilter.GroupResponsible = ddlGroup.SelectedItem != null && ddlGroup.SelectedItem.Value != "" ? ddlGroup.SelectedItem.Value : "";
            deliverableFilter.WorkgroupResponsible = ddlWorkgroup.SelectedItem != null && ddlWorkgroup.SelectedItem.Value != "" ? ddlWorkgroup.SelectedItem.Value : "";
            deliverableFilter.DeliverableTypeID = ddlDeliverable.SelectedItem != null && ddlDeliverable.SelectedItem.Value != "" ? Convert.ToDouble(ddlDeliverable.SelectedItem.Value) : 0;
            deliverableFilter.Market = ddlMarket.SelectedItem != null && ddlMarket.SelectedItem.Value != "" ? ddlMarket.SelectedItem.Value : "";
understand Let concept in Linq-- i think from Let it calculated the values with where conditio
3. Genrate Linq class using SiteEntitiesDataContext--DataContext
4. Gettting results in splistitemcollection using spquery.
4. filling deliverable entity with linq query from splistitemcollection.
5. Binding grid.


SPMetal

use SPMetal tool to generate entity class from Powershell command window. use entity classes with Linq to Sharepoint.

DataContext dc = new DataContext(SPContext.Current.Web.Url);

List<DeliverableItem> deliverable = InitialMilestoneListQuery();GridView1.DataSource = deliverable;
GridView1.DataBind();

  DataContext could be use to retrieve and update list item collections.

private List<DeliverableItem> InitialMilestoneListQuery()
        {
            List<DeliverableItem> deliverable = null;
            SPList objList = SPContext.Current.Web.Lists["Deliverable"];
            SPQuery objquery = new SPQuery();
            objquery.Query = "<Query></Query>";
            SPListItemCollection objColl = objList.GetItems(objquery);

            //var item1 = from SPListItem i in objColl
            //            //let expectedDateStart = ((i["Expected Assigned Date"] != null ? (i["Expected Assigned Date"] as DateTime?).Value.CompareTo("25/01/2011") >= 0 : false))
            //            //where i["Title"] == "Nelfinavir 2011 PSUR"
            //            select i;
            deliverable = (from SPListItem item in objColl
                           where item.ID < 100
                           select new DeliverableItem
                           {
                               Id = item.ID,
                               Title = item.Title,
                               PrimaryDeliverableOwner = item["Primary Deliverable Owner"] != null ? item["Primary Deliverable Owner"].ToString() : string.Empty,
                               AssociateDeliverableOwners = item["Associate Deliverable Owners"] != null ? item["Associate Deliverable Owners"].ToString() : string.Empty,
                               ExpectedAssignedDate = item["Expected Assigned Date"] as DateTime?,
                               ExpectedInitiationStartDate = item["Expected Initiation Start Date"] as DateTime?,
                               ExpectedContributionStartDate = item["Expected Contribution Start Date"] as DateTime?,
                               ExpectedAuthoringStartDate = item["Expected Authoring Start Date"] as DateTime?,
                               ExpectedReviewStartDate = item["Expected Review Start Date"] as DateTime?,
                               ExpectedApprovalStartDate = item["Expected Approval Start Date"] as DateTime?,
                               ExpectedHandoverDate = item["Expected Handover Date"] as DateTime?,
                               ActualAssignedDate = item["Actual Assigned Date"] as DateTime?,
                               ActualInitiationStartDate = item["Actual Initiation Start Date"] as DateTime?,
                               ActualContributionDate = item["Actual Contribution Date"] as DateTime?,
                               ActualAuthoringStartDate = item["Actual Authoring Start Date"] as DateTime?,
                               ActualReviewStartDate = item["Actual Review Start Date"] as DateTime?,
                               ActualApprovalDate = item["Actual Approval Date"] as DateTime?,
                               ActualHandoverDate = item["Actual Handover Date"] as DateTime?
                           }).ToList();
            return deliverable;
        }


 

Use of Let keyword

In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. You can do this with the let keyword, which creates a new range variable and initializes it with the result of the expression you supply. Once initialized with a value, the range variable cannot be used to store another value. However, if the range variable holds a queryable type, it can be queried.
class LetSample1
{
    static void Main()
    {
        string[] strings =
        {
            "A penny saved is a penny earned.",
            "The early bird catches the worm.",
            "The pen is mightier than the sword."
        };

        // Split the sentence into an array of words 
        // and select those whose first letter is a vowel. 
        var earlyBirdQuery =
            from sentence in strings
            let words = sentence.Split(' ')
            from word in words
            let w = word.ToLower()
            where w[0] == 'a' || w[0] == 'e'
                || w[0] == 'i' || w[0] == 'o'
                || w[0] == 'u'
            select word;

        // Execute the query. 
        foreach (var v in earlyBirdQuery)
        {
            Console.WriteLine("\"{0}\" starts with a vowel", v);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}


Most of the the samples avaialble here to

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

No comments:

Post a Comment