problem using IEnumerable

I obtained some code that is expecting IEnumerable arguments (see the screen shot below that shows the method overload in the intellisense).  I'm still reasonably new to .NET and not familiar with IEnumberable interfaces and how to use them.

How would I pass in these arguments to this method?

I've included the code below from the class files that make up the assembly.

Thanks for any help.
GenericEntry.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GoogleAnalytics.Request
{
    public class GenericEntry
    {
        #region Fields
 
        List<KeyValuePair<Dimension, string>> _dimensions = null;
 
        List<KeyValuePair<Metric, string>> _metrics = null;
 
        #endregion
 
        #region Properties
 
        public List<KeyValuePair<Dimension, string>> Dimensions
        {
            get { return _dimensions; }
 
            set { _dimensions = value; }
        }
 
        public List<KeyValuePair<Metric, string>> Metrics
        {
            get { return _metrics; }
 
            set { _metrics = value; }
        }
 
        #endregion
    }
}
 
Dimension.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GoogleAnalytics.Request
{
    public enum Dimension
    {
 
        browser = 0,
 
        browserVersion = 1,
 
        city = 2,
 
        connectionSpeed = 3,
 
        continent = 4,
 
        countOfVisits = 5,
 
        country = 6,
 
        date = 7,
 
        day = 8,
 
        daysSinceLastVisit = 9,
 
        flashVersion = 10,
 
        hostname = 11,
 
        hour = 12,
 
        javaEnabled = 13,
 
        language = 14,
 
        latitude = 15,
 
        longitude = 16,
 
        month = 17,
 
        networkDomain = 18,
 
        networkLocation = 19,
 
        pageDepth = 20,
 
        operatingSystem = 21,
 
        operatingSystemVersion = 22,
 
        region = 23,
 
        screenColors = 24,
 
        screenResolution = 25,
 
        subContinent = 25,
 
        userDefinedValue = 26,
 
        visitorType = 26,
 
        week = 27,
 
        year = 28,
 
        adContent = 29,
 
        adGroup = 30,
 
        adSlot = 31,
 
        adSlotPosition = 32,
 
        campaign = 33,
 
        keyword = 34,
 
        medium = 35,
 
        referralPath = 36,
 
        source = 37,
 
        exitPagePath = 38,
 
        landingPagePath = 39,
 
        pagePath = 40,
 
        pageTitle = 41,
 
        affiliation = 42,
 
        daysToTransaction = 43,
 
        productCategory = 44,
 
        productName = 45,
 
        productSku = 46,
 
        transactionId = 47,
 
        searchCategory = 48,
 
        searchDestinationPage = 49,
 
        searchKeyword = 50,
 
        searchKeywordRefinement = 51,
 
        searchStartPage = 52,
 
        searchUsed = 53
 
    }
}
 
Metric.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GoogleAnalytics.Request
{
    public enum Metric
    {
        bounces = 0,
 
        entrances = 1,
 
        exits = 2,
 
        newVisits = 3,
 
        pageviews = 4,
 
        timeOnPage = 5,
 
        timeOnSite = 6,
 
        visitors = 7,
 
        visits = 8,
 
        adCost = 9,
 
        adClicks = 10,
 
        CPC = 11,
 
        CPM = 12,
 
        CTR = 13,
 
        impressions = 14,
 
        uniquePageviews = 15,
 
        itemQuantity = 16,
 
        itemRevenue = 17,
 
        transactionRevenue = 18,
 
        transactions = 19,
 
        transactionShipping = 20,
 
        transactionTax = 21,
 
        uniquePurchases = 22,
 
        searchDepth = 23,
 
        searchDuration = 24,
 
        searchExits = 25,
 
        searchRefinements = 26,
 
        searchUniques = 27,
 
        searchVisits = 28,
 
        goal1Completions = 29,
 
        goal2Completions = 30,
 
        goal3Completions = 31,
 
        goal4Completions = 32,
 
        goalCompletionsAll = 33,
 
        goal1Starts = 34,
 
        goal2Starts = 35,
 
        goal3Starts = 36,
 
        goal4Starts = 37,
 
        goalStartsAll = 38,
 
        goal1Value = 39,
 
        goal2Value = 40,
 
        goal3Value = 41,
 
        goal4Value = 42,
 
        goalValueAll = 43
    }
}
 
ReportRequestor.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
using GoogleAnalytics.Request;
using GoogleAnalytics.Reports;
using System.Globalization;
 
 
namespace GoogleAnalytics.Request
{
    public class ReportRequestor
    {
        #region Fields
 
        private static readonly string requestUrlFormat = "https://www.google.com/analytics/feeds/data?ids={0}&dimensions={1}&metrics={2}&start-date={3}&end-date={4}";
        private static readonly string authUrlFormat = "accountType=GOOGLE&Email={0}&Passwd={1}&source=mysite.com-analyticsreader-1.0&service=analytics";
        private static CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
        private string _token = null;
        private string _username = null;
        private string _password = null;
 
        #endregion
 
 
        #region Constructor
 
        public ReportRequestor() 
        { 
        
        }
 
        public ReportRequestor(string email, string password)
        {
            _username = email;
 
            _password = password;
        }
 
        #endregion
 
 
        #region Properties
 
        public string Email
        {
            get { return _username; }
 
            set
            {
                if (!string.Equals(_username, value))
                {
                    _username = value;
 
                    _token = null;
                }
            }
        }
 
        public string Password
        {
            get { return _password; }
 
            set
            {
                if (!string.Equals(_password, value))
                {
                    _password = value;
 
                    _token = null;
                }
            }
        }
 
        #endregion
 
 
        #region Methods
 
        private string GetToken(string username, string password)
        {
            if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))
            {
                throw new ArgumentNullException("Username, Password", "Username and/or password not set");
            }
 
            string authBody = string.Format(authUrlFormat, username, password);
 
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin");
 
            req.Method = "POST";
 
            req.ContentType = "application/x-www-form-urlencoded";
 
            req.UserAgent = "Reimers.dk req";
 
            Stream stream = req.GetRequestStream();
 
            StreamWriter sw = new StreamWriter(stream);
 
            sw.Write(authBody);
 
            sw.Close();
 
            sw.Dispose();
 
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
 
            StreamReader sr = new StreamReader(response.GetResponseStream());
 
            string token = sr.ReadToEnd();
 
            string[] tokens = token.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
 
            foreach (string item in tokens)
            {
                if (item.StartsWith("Auth="))
                {
                    return item.Replace("Auth=", "");
                }
            }
 
            return string.Empty;
        }
 
        public IEnumerable<AnalyticsAccountInfo> GetAccounts()
        {
            if (string.IsNullOrEmpty(_token))
            {
                _token = GetToken(_username, _password);
            }
 
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/analytics/feeds/accounts/default");
 
            req.Headers.Add("Authorization: GoogleLogin auth=" + _token);
 
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
 
            Stream responseStream = response.GetResponseStream();
 
            StreamReader sr = new StreamReader(responseStream);
 
            string responseXml = sr.ReadToEnd();
 
            XDocument doc = XDocument.Parse(responseXml);
 
            XNamespace dxpSpace = doc.Root.GetNamespaceOfPrefix("dxp");
 
            XNamespace defaultSpace = doc.Root.GetDefaultNamespace();
 
 
 
            var entries = from en in doc.Root.Descendants(defaultSpace + "entry")
 
                          select new AnalyticsAccountInfo
 
                          {
 
                              AccountID = en.Elements(dxpSpace + "property").Where(xe => xe.Attribute("name").Value == "ga:accountId").First().Attribute("value").Value,
 
                              AccountName = en.Elements(dxpSpace + "property").Where(xe => xe.Attribute("name").Value == "ga:accountName").First().Attribute("value").Value,
 
                              ID = en.Element(defaultSpace + "id").Value,
 
                              Title = en.Element(defaultSpace + "title").Value,
 
                              ProfileID = en.Elements(dxpSpace + "property").Where(xe => xe.Attribute("name").Value == "ga:profileId").First().Attribute("value").Value,
 
                              WebPropertyID = en.Elements(dxpSpace + "property").Where(xe => xe.Attribute("name").Value == "ga:webPropertyId").First().Attribute("value").Value
 
                          };
 
            return entries;
 
        }
 
 
 
        private XDocument getReport(AnalyticsAccountInfo account, IEnumerable<Dimension> dimensions, IEnumerable<Metric> metrics, DateTime from, DateTime to)
        {
            if (string.IsNullOrEmpty(_token))
            {
                _token = GetToken(_username, _password);
            }
 
            StringBuilder dims = new StringBuilder();
 
            foreach (Dimension item in dimensions)
            {
                dims.Append("ga:" + item.ToString() + ",");
            }
 
            StringBuilder mets = new StringBuilder();
 
            foreach (Metric item in metrics)
            {
                mets.Append("ga:" + item.ToString() + ",");
            }
 
            string requestUrl = string.Format(requestUrlFormat, "ga:" + account.ProfileID, dims.ToString().Trim(",".ToCharArray()), mets.ToString().Trim(",".ToCharArray()), from.ToString("yyyy-MM-dd"), to.ToString("yyyy-MM-dd"));
 
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
 
            req.Headers.Add("Authorization: GoogleLogin auth=" + _token);
 
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
 
            Stream responseStream = response.GetResponseStream();
 
            string responseXml = new StreamReader(responseStream, Encoding.UTF8, true).ReadToEnd();
 
            XDocument doc = XDocument.Parse(responseXml);
 
            return doc;
        }
 
        public IEnumerable<GenericEntry> RequestReport(AnalyticsAccountInfo account, IEnumerable<Dimension> dimensions, IEnumerable<Metric> metrics, DateTime from, DateTime to)
        {
            XDocument doc = getReport(account, dimensions, metrics, from, to);
 
            XNamespace dxpSpace = doc.Root.GetNamespaceOfPrefix("dxp");
 
            XNamespace defaultSpace = doc.Root.GetDefaultNamespace();
 
            var gr = from r in doc.Root.Descendants(defaultSpace + "entry")
 
                    select new GenericEntry
 
                    {
 
                        Dimensions = new List<KeyValuePair<Dimension, string>>(
 
                            from rd in r.Elements(dxpSpace + "dimension")
 
                            select new KeyValuePair<Dimension, string>(
 
                                (Dimension)Enum.Parse(typeof(Dimension), rd.Attribute("name").Value.Replace("ga:", ""), true),
 
                                rd.Attribute("value").Value)),
 
                        Metrics = new List<KeyValuePair<Metric, string>>(
 
                            from rm in r.Elements(dxpSpace + "metric")
 
                            select new KeyValuePair<Metric, string>(
 
                                (Metric)Enum.Parse(typeof(Metric), rm.Attribute("name").Value.Replace("ga:", ""), true),
 
                                rm.Attribute("value").Value))
                    };
 
            return gr;
        }
 
 
 
        public IEnumerable<CityReport> GetUserCountByLocation(AnalyticsAccountInfo account, DateTime from, DateTime to)
        {
            IEnumerable<GenericEntry> report = RequestReport(account, new Dimension[] { Dimension.city, Dimension.latitude, Dimension.longitude }, new Metric[] { Metric.visits }, from, to);
 
            var cr = from r in report
 
                    select new CityReport
                    {
                        City = r.Dimensions.First(d => d.Key == Dimension.city).Value,
 
                        Latitude = Convert.ToDouble(r.Dimensions.First(d => d.Key == Dimension.latitude).Value, ci),
 
                        Longitude = Convert.ToDouble(r.Dimensions.First(d => d.Key == Dimension.longitude).Value, ci),
 
                        Count = Convert.ToInt32(r.Metrics.First(m => m.Key == Metric.visits).Value)
                    };
 
            return cr;
        }
 
        #endregion
    }
}

Open in new window

screenshot.png
-Dman100-Software ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nate FeinbergFinancial Center Operations ManagerCommented:
Many classes implement the IEnumerable interface. In order to pass something as an argument like such, it must implement the IEnumerable itnerface with the specified class. For example, your method call expects a class that implements IEnumerable (in other words, it supports iterating through multiple objects) of the class "Dimension", then the class "Metric". For example, see the following.
Where do you plan on getting the Dimension/Metric information, btw?
Hope it helps,
Nate

protected void Page_Load(Object sender, EventArgs evArgs)
{
     ReportRequestor req = new ReportRequestor(/*info*/);
     IEnumerable<AnalyticsAccountInfo> accounts = req.GetAccounts();
     AnalyticsAccountInfo acc = accounts.First(a => a.ProfileID == /*match*/);
     IEnumerable<Dimension> dims = /*Get collection of Dimension classes*/;
     IEnumerable<Metric> met = /*Get collection of Metric classes*/;
     lblPageViews.Text = req.RequestReport(acc, dims, met, /*Start Time*/, /*End Time*/);
}

Open in new window

0
-Dman100-Software ConsultantAuthor Commented:
Hi Nate,

Thanks for the help.

The Dimension/Metric information is in a public enum (see code below).

So, would I set the values in the IEnumerable like this:

IEnumerable<Dimension> dims = 41;
IEnumerable<Metric> met = 4;

Would that be correct?
Metric.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GoogleAnalytics.Request
{
    public enum Metric
    {
        bounces = 0,
 
        entrances = 1,
 
        exits = 2,
 
        newVisits = 3,
 
        pageviews = 4,
 
        timeOnPage = 5,
 
        timeOnSite = 6,
 
        visitors = 7,
 
        visits = 8,
 
        adCost = 9,
 
        adClicks = 10,
 
        CPC = 11,
 
        CPM = 12,
 
        CTR = 13,
 
        impressions = 14,
 
        uniquePageviews = 15,
 
        itemQuantity = 16,
 
        itemRevenue = 17,
 
        transactionRevenue = 18,
 
        transactions = 19,
 
        transactionShipping = 20,
 
        transactionTax = 21,
 
        uniquePurchases = 22,
 
        searchDepth = 23,
 
        searchDuration = 24,
 
        searchExits = 25,
 
        searchRefinements = 26,
 
        searchUniques = 27,
 
        searchVisits = 28,
 
        goal1Completions = 29,
 
        goal2Completions = 30,
 
        goal3Completions = 31,
 
        goal4Completions = 32,
 
        goalCompletionsAll = 33,
 
        goal1Starts = 34,
 
        goal2Starts = 35,
 
        goal3Starts = 36,
 
        goal4Starts = 37,
 
        goalStartsAll = 38,
 
        goal1Value = 39,
 
        goal2Value = 40,
 
        goal3Value = 41,
 
        goal4Value = 42,
 
        goalValueAll = 43
    }
}
 
Dimension.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GoogleAnalytics.Request
{
    public enum Dimension
    {
 
        browser = 0,
 
        browserVersion = 1,
 
        city = 2,
 
        connectionSpeed = 3,
 
        continent = 4,
 
        countOfVisits = 5,
 
        country = 6,
 
        date = 7,
 
        day = 8,
 
        daysSinceLastVisit = 9,
 
        flashVersion = 10,
 
        hostname = 11,
 
        hour = 12,
 
        javaEnabled = 13,
 
        language = 14,
 
        latitude = 15,
 
        longitude = 16,
 
        month = 17,
 
        networkDomain = 18,
 
        networkLocation = 19,
 
        pageDepth = 20,
 
        operatingSystem = 21,
 
        operatingSystemVersion = 22,
 
        region = 23,
 
        screenColors = 24,
 
        screenResolution = 25,
 
        subContinent = 25,
 
        userDefinedValue = 26,
 
        visitorType = 26,
 
        week = 27,
 
        year = 28,
 
        adContent = 29,
 
        adGroup = 30,
 
        adSlot = 31,
 
        adSlotPosition = 32,
 
        campaign = 33,
 
        keyword = 34,
 
        medium = 35,
 
        referralPath = 36,
 
        source = 37,
 
        exitPagePath = 38,
 
        landingPagePath = 39,
 
        pagePath = 40,
 
        pageTitle = 41,
 
        affiliation = 42,
 
        daysToTransaction = 43,
 
        productCategory = 44,
 
        productName = 45,
 
        productSku = 46,
 
        transactionId = 47,
 
        searchCategory = 48,
 
        searchDestinationPage = 49,
 
        searchKeyword = 50,
 
        searchKeywordRefinement = 51,
 
        searchStartPage = 52,
 
        searchUsed = 53
 
    }
}

Open in new window

0
Jaime OlivaresSoftware ArchitectCommented:
you are missing the following line:
using System.Collections;

it implements non-generic IEnumerable class.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Jaime OlivaresSoftware ArchitectCommented:
Sorry, disregard my last comment, I think it is not related with this case.
What you need is to cast the collection, like:

List<someClass> myList;

// invoke some method
SomeMethod(param1, param2, myList as IEnumerable<someClass>);
0
-Dman100-Software ConsultantAuthor Commented:
Hi jaime,

So, if I'm trying to get a report on pageview counts, it would be like this:

IEnumerable<Dimension> dims = 41;
IEnumerable<Metric> met = 4;

lblPageViews.Text = req.RequestReport(acc, dims, met, DateTime.Now, DateTime.Now);

but, like this:

List<Dimension> dims = 41;
List<Metric> met = 4;
lblPageViews.Text = req.RequestReport(acc, dims, met, DateTime.Now, DateTime.Now);

Is that correct?




0
Jaime OlivaresSoftware ArchitectCommented:
both proposals are the same!

Maybe you meant:
lblPageViews.Text = req.RequestReport(acc, dims as IEnumerable<Dimension>, met as IEnumerable<Metric>, DateTime.Now, DateTime.Now);
0
Jaime OlivaresSoftware ArchitectCommented:
by the way, this is not valid:
List<Dimension> dims = 41;
List<Metric> met = 4;

but:
List<Dimension> dims = new List<Dimension>();
then
dims.Add(someValueHere);
dims.Add(anotherValueHere);
//etcetera
0
Nate FeinbergFinancial Center Operations ManagerCommented:
41 and 4 are integer values, not IEnumerable collections. If you want the collections to contain only one element, try the following, or something similar. It just creates an array of one value, then "casts" using the as operator to IEnumerable for clarity. It's not actually required, in most cases, but safe to put it there anyway. You must also cast the number to the enum type, or choose the correct enumerated value. I recommend the latter.
Hope it helps,
Nate

lblPageViews.Text = req.RequestReport(acc,
     (new Dimension[1] { ((Dimension)41) }) as IEnumerable<Dimension>,
     (new Metric[1] { ((Metric)4) }) as IEnumerable<Metric>, DateTime.Now, DateTime.Now);

Open in new window

0
Jaime OlivaresSoftware ArchitectCommented:
just noticed in your original source code you have enumerations instead of List<> collection. In this case you can do the following:

lblPageViews.Text = req.RequestReport(acc,
    Enum.GetValues(typeof(Dimension)) as IEnumerable<Dimension>,
    Enum.GetValues(typeof(Metric)) as IEnumerable<Metric>,
    DateTime.Now, DateTime.Now);

Enum.GetValues will return you a collection based on members of enumeration, then you cast it as IEnumerable. This is, assuming that Dimension and Metric are enumerations.
0
-Dman100-Software ConsultantAuthor Commented:
Hi Jaime and Nate,

Well, I tried both examples and get the following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<GoogleAnalytics.Request.GenericEntry>' to 'string'. An explicit conversion exists (are you missing a cast?)

If it helps, I am trying to use the code in the following article for the .net google analytics api:

http://www.reimers.dk/blogs/jacob_reimers_weblog/archive/2009/05/09/added-google-analytics-reader-for-net.aspx

Thanks for all your help.  I sincerely appreciate it.
Regards.
0
Jaime OlivaresSoftware ArchitectCommented:
could you post the line where error occurs?
0
-Dman100-Software ConsultantAuthor Commented:
The error occurs on this line:

lblPageViews.Text = req.RequestReport(acc,
    Enum.GetValues(typeof(Dimension)) as IEnumerable<Dimension>,
    Enum.GetValues(typeof(Metric)) as IEnumerable<Metric>,
    DateTime.Now, DateTime.Now);

If it helps, the link I posted to the .net implementation for the google analytics api is what I'm using.

I compiled that code into an assembly and trying to create custom analytic reports.  I have gotten as far as authenticating successfully, but hit the wall with displaying the analytic report data.
0
Jaime OlivaresSoftware ArchitectCommented:
the problem is that req.RequestReports() is returning a value of type IEnumerable<GenericEntry> and you are trying to assign it to a string (lblPageViews.Text)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.