0
     public static IEnumerable<string> GetAllStreams()
                {       
                    using (SPSite site = new SPSite("`http://abzv67`"))
                    {
                        using (SPWeb web = site.OpenWeb("/"))
                        {
                            SPQuery query = new SPQuery();
                            query.Query = "<Query><Where><Eq><FieldRef Name='Code' /></Eq></Where></Query>"; 
                            string listUrl = "/Lists/Work Streams";
                            var list = web.GetList(listUrl);                    
                            return list.GetItems(query).Cast<string>();
                        }
                    }
                }

public static IEnumerable<string> GetAllDocs()
        {
            using (SPSite site = new SPSite("http://abzv67"))
            {
                using (SPWeb web = site.OpenWeb("/"))
                {
                    SPQuery query = new SPQuery();
                    query.Query = "<Query><Where><Eq><FieldRef Name='Code' /></Eq></Where></Query>"; 
                    string listUrl = "/Lists/Document Types";
                    var list = web.GetList(listUrl);
                    return list.GetItems(query).Cast<string>(); 
                }
            }
        }

I have the following method and I am trying to call this method as

List<string> codes = GetAllStreams().ToList();

I am getting the error as

"Unable to cast object of type 'Microsoft.SharePoint.SPListItem' to type 'System.String'."

How do I fix this ?

My Actual intention is to get the cartesian product of the two sp lists. Hence, I have written the following method.

public static IEnumerable<List<string>> GetCombinations(IEnumerable<List<string>> lists, IEnumerable<string> selected)
        {
            if (lists.Any())
            {
                var remainingLists = lists.Skip(1);
                foreach (var item in lists.First().Where(x => !selected.Contains(x)))
                foreach (var combo in GetCombinations(remainingLists, selected.Concat(new string[] { item })))
                yield return combo;
            }
            else
            {
                yield return selected.ToList();
            }
        }

I am using this method in my Web part (sandboxed) aimed to host on Office 365.

protected void Page_Load(object sender, EventArgs e)
        {
            List<string> codes = CombineLists.GetAllStreams().ToList();
            List<string> docs = CombineLists.GetAllDocs().ToList();
            List<List<string>> lists = new List<List<string>> { codes, docs };
            var combos = CombineLists.GetCombinations(lists, new List<string>()).Distinct();
            foreach (var combo in combos)
                msgLabel.Text = string.Join("-", combo.Select(x => x.ToString()));
            msgLabel.Visible = true;
            return;
        } 

My Aim is to get all the possible combinations of the list items in two lists. One list is in this format.

("00.0","01.0","02.0","03.0","04.0","04.1","04.2","04.3","04.4","04.5","05.0","05.1","05.2","06.0","07.0","08.0","09.0","10.0","10.1","10.2","10.3","10.4","10.5","11.0","11.1","11.2","11.3","11.4","12.0","12.1","12.2","13.0","14.0","15.0","16.0","17.0","18.0","18.1","19.0","20.0","21.0","21.1","21.2","21.3","22.0","23.1","23.2","23.3","23.4","23.5","24.0","24.1","24.2","25.0","25.1","25.2","26.1","26.2","26.3","26.4","26.5","27.1","27.2","27.3","27.4","28.1","28.2","29.1","29.2","30.1","30.2","30.3","30.4","30.5","31.0")

Other list is in this format.

("PL","PR","FO","CE","OC","JD","TD","LD")

I need the final string of all the possible combination as (00.0-PL) and add them in another list.

5
  • What are you trying to do? Why are you trying to convert full list items to a single string? May 5 '14 at 10:23
  • Yes. I am trying to convert them as string list
    – Ram
    May 5 '14 at 10:29
  • The main question was why you triy to do this:) May 5 '14 at 10:40
  • Trying to get the combination of Two lists as Cartesian Product.
    – Ram
    May 5 '14 at 10:44
  • public static IEnumerable<List<string>> GetCombinations(IEnumerable<List<string>> lists, IEnumerable<string> selected) { if (lists.Any()) { var remainingLists = lists.Skip(1); foreach (var item in lists.First().Where(x => !selected.Contains(x))) foreach (var combo in GetCombinations(remainingLists, selected.Concat(new string[] { item }))) yield return combo; } else { yield return selected.ToList(); } }
    – Ram
    May 5 '14 at 10:44
1

Take list items in datatable or splistitemcollections and then merge another list with datatable.

You can use:

DataTable dt = oList.Items.GetDataTable(); 

or

SPListItemCollection items = list.GetItems(spq); 
DataTable dt = items.GetDataTable();
5
  • Please add more details regarding how to do what you propose. Right now your answer is not as helpful as it could be :) May 5 '14 at 11:10
  • DataTable dt = oList.Items.GetDataTable(); or SPListItemCollection items = list.GetItems(spq); dtItems = items.GetDataTable();
    – Richa Jain
    May 5 '14 at 11:12
  • Updated your answer with that info :) May 5 '14 at 11:14
  • Dear Robert, I have updated the whole request again. Hope that make sense. Please help me.
    – Ram
    May 5 '14 at 11:17
  • Could some one help me with this please?
    – Ram
    May 7 '14 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.