Random Unique Numbers in C#

Right, then in that case:

Code:
if (list.items.Any()) {
  var numberToTake = 5 / list.items.Count() * 100;
  var theFivePercentOfRecords = list.items.OrderBy(each => Guid.NewGuid()).Take(numberToTake);
}
 
You will possibly cast the list.items.count() to a double so that it will allow a decimal value on the division.

Code:
var numberToTake = ((5 / (double)list.Items.Count()) * 100);
var theFivePercentOfRecords = list.Items.OrderBy(each => Guid.NewGuid()).Take((int)numberToTake);
 
I didn't mate, not really looked at it since. Swamped with other stuff!

I'm going to try and look at it again this week, so expect more responses in this thread!
 
Here it is mate, sorry for the delay!

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare sites
            SPSite sourceSite = new SPSite(@"http://sharepoint");

            //Declare webs
            SPWeb sourceWeb = sourceSite.RootWeb;

            //Declare lists
            SPLists sourceList = sourceWeb.GetList("/Lists/Staff Contacts");

            SPListItemCollection sourceItems = sourceList.Items;

            try
            {
                if (sourceList.Items.Any())
                {
                    var numberToTake = 5 / sourceList.Items.Count() * 100;
                    var thePercentOfRecords = sourceList.Items.OrderBy(each => Guid.NewGuid()).Take(numberToTake);
                }

                foreach (int n in thePercentOfRecords)
                {
                    SPListItem item = sourceList.Items[n];
                    String strFirstName = item["First Name"].ToString();
                    String strLastName = item["Last Name"].ToString();
                    String strFullName = strFirstName + " " + strLastName;
                    String strTeam = item["Team"].ToString();

                    CopyItem(strFullName, strTeam);

                }

                sourceWeb.Dispose();
                sourceSite.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        static void CopyItem(String name, String team)
        {
            //Declare sites
            SPSite destSite = new SPSite(@"http://sharepoint/communications");

            //Declare webs
            SPWeb destWeb = destSite.OpenWeb();

            //Declare lists
            SPList destList = destWeb.Lists["Staff Satisfaction"];

            SPListItemCollection listItems = destList.Items;

            SPListItem item = listItems.Add();

            item["Staff Member"] = name;
            item["Team"] = team;

            item.Update();

            destWeb.Dispose();
            destSite.Dispose();
        }

    }
}
 
I think it is only the 2010 Object Model version that implements IEnumerable so this won't work for MOSS 2007 code. Will have a look in the VM later and see if I can get something working.
 
From MSDN:

SharePoint 2010....
public class SPListItemCollection : SPBaseCollection, IEnumerable

WSS 3.0...
public class SPListItemCollection : SPBaseCollection

Does that not suggest that only 2010 implements IEnumerable?

I'm trying this in a VM and have added references to System.Core.dll and added using System.Linq and it doesn't work.
 
Give this a try. I haven't tested in my VM as I don't have the same SP objects/lists as you do but it builds and should work.

You will need to add some more error handling. I hope DJ doesn't critique my crappy code!

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Declare sites
                SPSite sourceSite = new SPSite(@"http://sharepoint");

                //Declare webs
                SPWeb sourceWeb = sourceSite.RootWeb;

                //Declare lists
                SPList sourceList = sourceWeb.GetList("/Lists/Staff Contacts");

                SPListItemCollection sourceItems = sourceList.Items;

                //Get a list of integers (5% of total)
                var queryset = RandomGenerator(0, sourceItems.Count, 5);

                foreach (int n in queryset)
                {
                    SPListItem item = sourceList.Items[n];
                    String strFirstName = item["First Name"].ToString();
                    String strLastName = item["Last Name"].ToString();
                    String strFullName = strFirstName + " " + strLastName;
                    String strTeam = item["Team"].ToString();

                    CopyItem(strFullName, strTeam);
                }

                sourceWeb.Dispose();
                sourceSite.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private static void CopyItem(String name, String team)
        {
            //Declare sites
            SPSite destSite = new SPSite(@"http://sharepoint/communications");

            //Declare webs
            SPWeb destWeb = destSite.OpenWeb();

            //Declare lists
            SPList destList = destWeb.Lists["Staff Satisfaction"];

            SPListItemCollection listItems = destList.Items;

            SPListItem item = listItems.Add();

            item["Staff Member"] = name;
            item["Team"] = team;

            item.Update();

            destWeb.Dispose();
            destSite.Dispose();
        }

        private static List<int> RandomGenerator(int start, int count, int percentage)
        {
            //Create two new lists
            List<int> list = new List<int>();
            List<int> result = new List<int>();

            //Populate a temporary list with all the possible values between start value + count
            for (int i = start; i < count; i++)
            {
                list.Add(i);
            }

            if (list.Any())
            {
                //Get the number of required values
                var numberToTake = ((percentage / (double)list.Count()) * 100);

                //Get a subset of the total
                var records = list.OrderBy(each => Guid.NewGuid()).Take((int)numberToTake);

                //Add the results to a list and return
                foreach (int i in records)
                {
                    result.Add(i);
                }
                return result;
            }
            else {
                return null;
            }
        }

    }
}
 
From MSDN:

SharePoint 2010....
public class SPListItemCollection : SPBaseCollection, IEnumerable

WSS 3.0...
public class SPListItemCollection : SPBaseCollection

Does that not suggest that only 2010 implements IEnumerable?

I'm trying this in a VM and have added references to System.Core.dll and added using System.Linq and it doesn't work.

It implements SPBaseCollection, which implements ICollection, which extends IEnumerable.

It's not possible to use foreach on anything that doesn't implement IEnumerable. Try it. The error message will tell you exactly that.
 
It implements SPBaseCollection, which implements ICollection, which extends IEnumerable.

It's not possible to use foreach on anything that doesn't implement IEnumerable. Try it. The error message will tell you exactly that.

The error message I was getting was this:

'Microsoft.SharePoint.SPListItemCollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'Microsoft.SharePoint.SPListItemCollection' could be found (are you missing a using directive or an assembly reference?)
 
The error message I was getting was this:

'Microsoft.SharePoint.SPListItemCollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'Microsoft.SharePoint.SPListItemCollection' could be found (are you missing a using directive or an assembly reference?)
 
I would have said no to that!

Edit: I have added a reference to System.Core.dll which according to MSDN article I read is what is needed for System.Linq.
 
Last edited:
Balls, just spotted what it is. .Any() is only supported on IEnumerable<T> not IEnumerable.

Revert to the old fashioned way:

Code:
if (list.items.Count > 0)
 
Back
Top Bottom