The query operator ElementAt is not supported LINQ

Why am I getting the following error ?
The query operator ElementAt is not supported
using (TimeZoneDataContext TimeZoneCtx = new TimeZoneDataContext(GlobalMethods.SqlConnectionString))
                {
                    var rsTimeZone = (from t in TimeZoneCtx.TimeZones
                                  where t.TimeZoneGroupID == tzId
                                  orderby t.TimeZoneID
                                  select t);
                   
 
                    for (int i = 0; i <= rsTimeZone.Count(); i++)
                    {                        
                        switch (i)
                        {
                            case 0:
                                var rsOne = rsTimeZone.ElementAt(1);
                                mtxtTimeZone1Start.Text = rsOne.StartTime.Value.TimeOfDay.ToString();
                                //mtxtTimeZone1Start.Text = ((DateTime)rsTimeZone.ElementAt(i).StartTime.Value).TimeOfDay.ToString();
                                //mtxtTimeZone1Stop.Text = rsTimeZone.ElementAt(0).EndTime.Value.ToString();
                                //((DateTime)result.BeforeShiftStart1).TimeOfDay.ToString();
 
                                break;
 
                        }
 
                    }
}

Open in new window

u2envy1Asked:
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.

deadlyDevCommented:
Change your code to:
using (TimeZoneDataContext TimeZoneCtx = new TimeZoneDataContext(GlobalMethods.SqlConnectionString))
            {
                var rsTimeZone = (from t in TimeZoneCtx.TimeZones
                                  where t.TimeZoneGroupID == tzId
                                  orderby t.TimeZoneID
                                  select t);
                bool first = true;
                foreach (var tz in rsTimeZone)
                {
                    if (first)
                    {
                        mtxtTimeZone1Start.Text = tz.StartTime.Value.TimeOfDay.ToString();
                    }
                }
            }

Open in new window

0
u2envy1Author Commented:
I would like to get a specific index from the collection.........
 switch (i)
                        {
                            case 4:
                                var rsOne = rsTimeZone.ElementAt(i);
                                mtxtTimeZone2Start.Text = rsOne.StartTime.Value.TimeOfDay.ToString();
                               
                                 
                                break;
 
                        }

Open in new window

0
deadlyDevCommented:
In that case change the first snippet to the second...
var rsTimeZone = (from t in TimeZoneCtx.TimeZones
                                  where t.TimeZoneGroupID == tzId
                                  orderby t.TimeZoneID
                                  select t);
 
var rsTimeZone = (from t in TimeZoneCtx.TimeZones
                                  where t.TimeZoneGroupID == tzId
                                  orderby t.TimeZoneID
                                  select t).ToList();

Open in new window

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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Fernando SotoRetiredCommented:
ElementAt can only be applied to collections of Enumerable. In your posted code you would have a single element of the result set.

You can do the following to get the row of data at ElementAt 1
var rsOne = rsTimeZone.ToList().ElementAt(1);
0
deadlyDevCommented:
I must disagree Fernando...

ElementAt CAN be supported on the IQueryable interface, it only depends on whether the underlying datasource can process iit. In this case, the Linq to SQL provider it is not.
0
u2envy1Author Commented:
Thx, its exactly what I wanted..
0
Fernando SotoRetiredCommented:
@ deadlyDev;

Seeming that this was a Linq to SQL and the type returned is of type IEnumerable, ElementAt can only work with a collection of Enumerable.

I agree that collections of IQueryable impement a version of ElementAt and can be used on collections of IQueryable.
0
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
.NET Programming

From novice to tech pro — start learning today.