Mike Diaz
Mike Diaz

Reputation: 2065

Linq Query Giving Error

I am getting this error when I use the Linq expression of

var emp = _testModel.Where(m => m.Date == DateTime.Now).Select(m=>m);

The error is

'System.Collections.Generic.IEnumerable<TestModel>' does not contain a definition for 'System' and no extension method 'System' accepting a first argument of type 'System.Collections.Generic.IEnumerable<TestModel>' could be found (are you missing a using directive or an assembly reference?). 

I have google'd and looked everywhere by I have no idea what it is talking about? It doesn't throw an exception. The only way I found out about this was stepping through the expression.

Namespaces that are imported

using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

Tried something like this and it still gives erros

string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

    var shortDigits = digits.Where((digit, index) => digit.Length < index);

Upvotes: 1

Views: 303

Answers (4)

Mike Diaz
Mike Diaz

Reputation: 2065

Turns out there is a bug in the Windows phone 7 series and it was preventing me from looking into the results.

Upvotes: 0

Foole
Foole

Reputation: 4850

It must be a problem in part of the code you're not showing us. This code compiles fine using .NET 3.5:

using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

class TestModel
{
    public DateTime Date { get; set; }
}

class Test
{
    public void TestFunction()
    {
        IEnumerable<TestModel> _testModel = new TestModel[] { new TestModel() };
        var emp = _testModel.Where(m => m.Date == DateTime.Now).Select(m => m);

        string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
        var shortDigits = digits.Where((digit, index) => digit.Length < index);
    }
}

Upvotes: 1

Cornelius
Cornelius

Reputation: 3616

Could be that _testModel is of type IEnumerable and not IEnumerable(Of T) the non generic IEnumerable does not have an extension method Where.

You could try

var emp = _testModel.OfType<T>().Where(p => p.Date == DateTime.Now);

where T should be the type of the generic enumerable you want in this case the type that you want p to be.

Upvotes: 0

Graviton
Graviton

Reputation: 83254

maybe you should put

using System.Linq;

at the start of your cs file.

See here for a similar case.

Upvotes: 2

Related Questions