Select properties from list items

02/04/2014 11:58

///get id and task display name from SPListItemCollection 

public static Dictionary<int, string> LoadTasksForSelectedUser2(SPWeb web, string userName)
        {
            Dictionary<int, string> taskIds = new Dictionary<int, string>();
            SPList taskList = web.Lists.TryGetList("Custom Task List");
            if (taskList != null)
            {
                SPUser user = web.EnsureUser(userName);
                if (user != null)
                {
                    SPListItemCollection items = taskList.GetItems(new SPQuery
                    {
                        Query =
                              @"  
                           <Where>
                            <And>
                              <Eq>
                                 <FieldRef Name='AssignedTo' LookupId='True'/>
                                 <Value Type='Integer'>" + user.ID + @"</Value>
                                </Eq>
                                <Neq>
                                 <FieldRef Name='PercentComplete'/>
                                 <Value Type='Number'>1</Value>
                               </Neq>
                           </And>
                           </Where>"

                    });
                    if (items.Count > 0)
                    {
                        taskIds = items.OfType<SPListItem>().Select(i => new {i.ID, i.DisplayName}).ToDictionary(i => i.ID, i => i.DisplayName);
                    }
                }
            }
            return taskIds;
        }


///get only task ids

 public static List<int> LoadTasksForSelectedUser(SPWeb web, string userName)
        {
            List<int> taskIds = new List<int>();
            SPList taskList = web.Lists.TryGetList("Custom Task List");
            if (taskList != null)
            {
                SPUser user = web.EnsureUser(userName);
                if (user != null)
                {
                    SPListItemCollection items = taskList.GetItems(new SPQuery
                    {
                        Query =
                              @"  
                           <Where>
                            <And>
                              <Eq>
                                 <FieldRef Name='AssignedTo' LookupId='True'/>
                                 <Value Type='Integer'>" + user.ID + @"</Value>
                                </Eq>
                                <Neq>
                                 <FieldRef Name='PercentComplete'/>
                                 <Value Type='Number'>1</Value>
                               </Neq>
                           </And>
                           </Where>"

                    });
                    if (items.Count > 0)
                    {
                        taskIds = items.OfType<SPListItem>().Select(item => item.ID).ToList();
                    }
                }
            }
            return taskIds;
        }