Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

problem using IEnumerable

Posted on 2009-07-02
13
Medium Priority
?
817 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 8

Expert Comment

by:Nate Feinberg
ID: 24764210
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
 

Author Comment

by:-Dman100-
ID: 24764434
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24764791
you are missing the following line:
using System.Collections;

it implements non-generic IEnumerable class.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24764835
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
 

Author Comment

by:-Dman100-
ID: 24765201
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24765335
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24765345
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
 
LVL 8

Expert Comment

by:Nate Feinberg
ID: 24765351
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24765484
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
 

Author Comment

by:-Dman100-
ID: 24768068
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24768222
could you post the line where error occurs?
0
 

Author Comment

by:-Dman100-
ID: 24770119
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 24770299
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question