无法通过查询LINQ从DDS获取项目

时间:2013-04-18 14:34:39

标签: c# .net episerver

我是C#ASP.NET的新手,我正在尝试从商店(EPiServer)获取商品。

Visual Studio说

  

无法解析符号Where,OnderzoekId和ToList

我做错了什么?我使用了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>();
    }
}

2 个答案:

答案 0 :(得分:2)

linq声明

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

尝试枚举集合,但GetStore()方法返回单个项目。尝试使用以下代码代替GetOnderzoekColumns方法(未经测试)

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();
}

我将以下extension methods添加到您的解决方案中,然后您可以使用强类型的Find方法,它将比上面的方法更有效,它返回所有项目,然后使用linq在内存中过滤器Where ()方法。

答案 1 :(得分:0)

我使用以下代码来使其工作。我也删除了AutomaticallyCreateStore和AutomaticallyRemapStore属性。

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();
}