0

This is my code:

private List<Picture> items;

public List<Picture> Items
{
    get
    {
        if (items == null)
        {
            items = new List<Picture>();
            items = this.LoadAllItems();
        }

        return items;
    }

    set
    {
        this.items = value;
    }
}

public List<Picture> GetItemsBySearch(string searchTerm, string fieldName)
{
    DataAccess dbcontext = new DataAccess();
    string internalfield = dbcontext.GetInternalFieldNameByDisplayName(fieldName);
    List<Picture> PictureHits = new List<Picture>();

    var data = from Picture item in this.Items
               where item.GetType().GetProperty(internalfield).GetValue(item, null).ToString().Contains(searchTerm)
               select item;  

    foreach (Picture item in data)
    {
        PictureHits.Add(item);
    }

    return PictureHits;
}

The data.Count contains 88 items but it does not enter a foreach loop.

Does the var not work within a foreach loop?

Picture is a class with some objects like filename, ID, etc.

enter image description here

This is what "data" contains so it contains 88 items doesnt it?

12
  • 2
    from Picture item in this.Items??? should be from item in this.Items Commented Sep 13, 2017 at 10:59
  • 1
    Just a remark: the line items = new List<Picture>(); in the getter is useless. You can just remove it. Commented Sep 13, 2017 at 11:00
  • 2
    "The data.Count contains 88 items" No, it shows that source contains 88 items, which is the original List, aka Items. Data has no Count property. Commented Sep 13, 2017 at 11:20
  • 1
    if I try PictureHits = data.ToList(); PictureHits Count is 0 Maybe my where is not working? Commented Sep 13, 2017 at 11:24
  • 2
    @Julian yes, the where is not working Commented Sep 13, 2017 at 11:25

1 Answer 1

2

Problem might be in where condition

var data = from Picture item in this.Items
               where item.GetType().GetProperty(internalfield) // threre
                   .GetValue(item, null).ToString().Contains(searchTerm) // might bean issue
               select item;  

try to comment it and repro. If foreach will work, then you have criteria which is filtered all your data.

Sign up to request clarification or add additional context in comments.

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.