0

I am new to C# ASP.NET and I am trying to get the items from a store (EPiServer).

Visual Studio says

Cannot resolve symbol Where, OnderzoekId and ToList

What am I doing wrong? I used this code example:

[EPiServerDataStore(AutomaticallyCreateStore = true, AutomaticallyRemapStore = true)]
public class OnderzoekColumn
{
    private static int Counter = 0;
    public Identity Id { get; set; }
    public int ColumnId { get; set; }
    public int OnderzoekId { get; set; }
    public string ColumnName { get; set; }

    public OnderzoekColumn()
    {
        Initialize();
    }

    public OnderzoekColumn(int onderzoekId, string columnName)
    {
        Initialize();

        OnderzoekId = onderzoekId;
        ColumnName = columnName;
    }

    protected void Initialize()
    {
        Id = Identity.NewIdentity(Guid.NewGuid());
        ColumnId = System.Threading.Interlocked.Increment(ref Counter);
        OnderzoekId = 0;
        ColumnName = string.Empty;
    }

    public static List<OnderzoekColumn> GetOnderzoekColumns(int onderzoekId)
    {
        var store = typeof(OnderzoekColumn).GetStore();

        var columns = from c in store
                      where c.OnderzoekId == onderzoekId
                      select c;

        if (columns == null)
        {
            return new List<OnderzoekColumn>();
        }

        return columns.ToList<OnderzoekColumn>();
    }
}
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
  • What does `GetStore` do? – Arran Apr 18 '13 at 14:39
  • As the class was decorated with the EPiServerDataStoreAttribute and the AutomaticallyCreateStore property was set to true, the call to GetStore will trigger the store creation automatically. – user2132291 Apr 18 '13 at 15:12

2 Answers2

2

The linq statement

var columns = from c in store
              where c.OnderzoekId == onderzoekId
              select c;

is trying to enumerate over a collection, but the GetStore() method returns a single item. Try using the following code in place of your GetOnderzoekColumns method (its untested)

public static List<OnderzoekColumn> GetOnderzoekColumns(int onderzoekId)
{
  var store = typeof(OnderzoekColumn).GetStore();

  var columns = store.Items<OnderzoekColumn>().Where(c => c.OnderzoekId == onderzoekId);

  return columns.ToList();
}

I'd add the following extension methods to your solution, then you can use a strongly typed Find method, which will be more efficient than the above, which returns all items, then filters in memory using the linq Where() method.

tompipe
  • 949
  • 6
  • 8
  • I tried your solution already and it didn't work. I removed the AutomaticallyCreateStore attribute and now it works great! – user2132291 Apr 19 '13 at 12:09
0

I used the following code to get it to work. I removed the AutomaticallyCreateStore and AutomaticallyRemapStore attribute also.

public static List<OnderzoekColumn> GetOnderzoekColumns(int onderzoekId)
{
 var store = DynamicDataStoreFactory.Instance.GetStore(typeof(OnderzoekColumn));

 var query = from item in store.Items<OnderzoekColumn>()
             where item.OnderzoekId == onderzoekId
             select item;

 return query.ToList();
}