0

So I have this model setup

public class ListOfThings
{
    public int Id {get;set;}
    public List<int> DefaultRanks {get;set;}

    static public IEnumerable<ListOfThings> ListOfDefault{
       get {
            listOfDefault = new List<ListOfDefault>();
            listOfDefault.Add(N1);
            listOfDefault.Add(N2);
            listOfDefault.Add(N3);
            return listOfDefault.AsReadOnly();
       }
    }

    static public ListOfThings N1{ get { return new ListOfThings { Id = 1, Addend = new List<int> { 1 }}; } }  
    static public ListOfThings N2{ get { return new ListOfThings { Id = 2, Addend = new List<int> { 1,2 }}; } }
    static public ListOfThings N3{ get { return new ListOfThings { Id = 3, Addend = new List<int> { 1,2,3 }}; } }

    static private ListOfThings n1;
    static private ListOfThings n2;
    static private ListOfThings n3;
    static private List<ListOfThings> listOfDefault;
}

And whenever I try to query that static Ienumerable, it'll give me a null value.

var test = ListOfThings.ListOfDefault.Where(a => a.Id == 2);

And if I try to do this, it'll give me a cast warning.

ListOfThings test = ListOfThings.ListOfDefault.Where(a => a.Id == 2);

Any advice is appreciated..

Thanks!! -G

1
  • 1
    What does any of this have to do with ASP.NET, MVC or Entity Framework? It's just LINQ to Objects as far as I can see... Commented Jan 26, 2012 at 9:46

3 Answers 3

2

It's not clear what you mean by "it'll give me a null value" - I would dispute that, with the code you've given. (A short but complete program demonstrating the problem would really help.)

The second line will definitely give a compile time failure, on the grounds that Where returns an IEnumerable<ListOfThings>, not a single ListOfThings. You may want something like:

ListOfThings test = ListOfThings.ListOfDefault.SingleOrDefault(a => a.Id == 2);

Note that you're never initializing or using n1, n2 or n3 in the code you've given. I'd also suggest that an iterator block would make your ListOfDefault property simpler:

static public IEnumerable<ListOfThings> ListOfDefault {
   get {
        yield return N1;
        yield return N2;
        yield return N3;
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply, I think that was what I was missing but instead of "SingleOrDefault" the "FirstOrDefault" worked. Well, the "null exception error" I was getting was pointin to the "a.Id==2" area whenever I debug. wasn't really sure on what was happening until you mentioned the SingleOrDefault! Thanks!
@gdubs: You'd get a NullReferenceException if you used n1 instead of N1 etc... is it possible that that's what was happening?
hmm..what do you mean? like "yield return n1"? well, I took out the private statics (eg n1, n2) and just left the public ones and tried the old line and it's still doing it. What is the difference between "yield return" and just using the private static list that i have and adding the values to it? i'm kinda curious as i saw this on the "validate" method of the Ivalidatable attribute, the way it adds values is by using "yield return new ValidationResult"..
@gdubs: Yes - if you were using n1, that would have given you a null value, because it was never initialized. Again, if you're having problems then a short but complete program would be very useful. As for using yield return instead of using a static variable: currently your code isn't thread-safe, for one thing...
no i think i got the whole thing working. was just really curious about that yield return. so is it safer than the way i was doing it? "return listOfDefault.AsReadOnly();" i was just reading that it saves up memory or something better than the one i was using..
|
1

There are some problems in your code. The changes I made are:

// in a getter
listOfDefault = new List<ListOfDefault>(); 
// should be:
listOfDefault = new List<ListOfThings>();

// static getters
static public ListOfThings N1{ get { return new ListOfThings { Id = 1, Addend = new List<int> { 1 }}; } }  
static public ListOfThings N2{ get { return new ListOfThings { Id = 2, Addend = new List<int> { 1,2 }}; } }
static public ListOfThings N3{ get { return new ListOfThings { Id = 3, Addend = new List<int> { 1,2,3 }}; } }
// should be:

static public ListOfThings N1 { get { return new ListOfThings { Id = 1, DefaultRanks = new List<int> { 1 } }; } }
static public ListOfThings N2 { get { return new ListOfThings { Id = 2, DefaultRanks = new List<int> { 1, 2 } }; } }
static public ListOfThings N3 { get { return new ListOfThings { Id = 3, DefaultRanks = new List<int> { 1, 2, 3 } }; } }

And the most important is you don't call the FirstOrDefault() on your LINQ (if you want to get more objects use ToList() ):

var test = ListOfThings.ListOfDefault.Where(a => a.Id == 2).FirstOrDefault();

This way it should work.

2 Comments

Yeah I saw what I was missing.. Thanks! Btw, what's the difference between using ".Where(x => x...).FirstOrDefault()" and ".FirstOrDefault(x => x....)" ? sorry kind of a noob on this..
@gdubs: There's no difference - the latter is simpler IMO.
1

I tried recreating the issue there were few problems. I have pasted the code below

public class ListOfThings
{
    public int Id { get; set; }
    public List<int> DefaultRanks { get; set; }

    static public IEnumerable<ListOfThings> ListOfDefault
    {
        get
        {
            listOfDefault = new List<ListOfThings>();
            listOfDefault.Add(N1);
            listOfDefault.Add(N2);
            listOfDefault.Add(N3);
            return listOfDefault.AsReadOnly();
        }
    }

    static public ListOfThings N1 { get { return new ListOfThings { Id = 1, DefaultRanks = new List<int> { 1 } }; } }
    static public ListOfThings N2 { get { return new ListOfThings { Id = 2, DefaultRanks = new List<int> { 1, 2 } }; } }
    static public ListOfThings N3 { get { return new ListOfThings { Id = 3, DefaultRanks = new List<int> { 1, 2, 3 } }; } }

    static private ListOfThings n1;
    static private ListOfThings n2;
    static private ListOfThings n3;
    static private List<ListOfThings> listOfDefault;
}



class Program
{
    static void Main(string[] args)
    {
        var test = ListOfThings.ListOfDefault.Where(a => a.Id == 2);
        IEnumerable<ListOfThings> ds = ListOfThings.ListOfDefault.Where(a => a.Id == 2);

    }
}


ListOfThings test = ListOfThings.ListOfDefault.Where(a => a.Id == 2);

is returning Ienumerable not the single objectif you want single or first object do something like

 ListOfThings ds = ListOfThings.ListOfDefault.Where(a => a.Id == 2).FirstOrDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.