无法将var转换为List <mytype> </mytype>

时间:2014-01-30 16:37:53

标签: entity-framework list casting frameworks entity

任何人都可以帮我处理以下代码吗?

        var returned= MyDBContext.MyEntities.Select(c => new
        {
            c.Property1,
            c.Property2,
            c.Property3
        })
        .Where(c => c.Property1.Contains(anyStringParam))
        .OrderBy(c => c.Property1)
        .Take(100)
        .ToList();

         List<MyType>list = returned;

我在这个论坛上看到了一些答案,但我仍有问题。

1 个答案:

答案 0 :(得分:0)

试试这个......

    var returned= MyDBContext.MyEntities.Select(c => new
    {
        Property1 = c.Property1,
        Property2 = c.Property2,
        Property3 = c.Property3
    })
    .Where(c => c.Property1.Contains(anyStringParam))
    .OrderBy(c => c.Property1)
    .Take(100)
    .AsEnumerable().Select(c => new MyType
    {
        Property1 = c.Property1,
        Property2 = c.Property2,
        Property3 = c.Property3
    }).ToList();

结果应该是返回现在是 MyType列表