有一种名为Product的实体类型是由实体框架生成的。 我写了这个问题

public IQueryable<Product> GetProducts(int categoryID)
{
    return from p in db.Products
           where p.CategoryID== categoryID
           select new Product { Name = p.Name};
}

下面的代码抛出以下错误:

实体或复杂类型的Shop。产品不能构造在 LINQ到实体查询"

var products = productRepository.GetProducts(1).Tolist();

但是当我使用select p而不是select new Product {Name = p.Name};它工作正常。

如何执行自定义选择节?


当前回答

为了回应另一个被标记为重复的问题(见这里),我根据Soren的答案想出了一个快速而简单的解决方案:

data.Tasks.AddRange(
    data.Task.AsEnumerable().Select(t => new Task{
        creator_id   = t.ID,
        start_date   = t.Incident.DateOpened,
        end_date     = t.Incident.DateCLosed,
        product_code = t.Incident.ProductCode
        // so on...
    })
);
data.SaveChanges();

注意: 这个解决方案只适用于Task类上有一个导航属性(外键)(这里称为“Incident”)。 如果你没有,你可以使用“AsQueryable()”其他发布的解决方案之一。

其他回答

在许多情况下,并不需要这种转换。考虑一下您需要强类型List的原因,并评估您是否只是需要数据,例如,在web服务中或用于显示数据。这与类型无关。 您只需要知道如何读取它,并检查它是否与您定义的匿名类型中定义的属性相同。这是最优的情况,因为你不需要实体的所有字段,这就是匿名类型存在的原因。

一个简单的方法是这样做:

IEnumerable<object> list = dataContext.Table.Select(e => new { MyRequiredField = e.MyRequiredField}).AsEnumerable();

另一种简单的方法:)

public IQueryable<Product> GetProducts(int categoryID)
{
    var productList = db.Products
        .Where(p => p.CategoryID == categoryID)
        .Select(item => 
            new Product
            {
                Name = item.Name
            })
        .ToList()
        .AsQueryable(); // actually it's not useful after "ToList()" :D

    return productList;
}

为了回应另一个被标记为重复的问题(见这里),我根据Soren的答案想出了一个快速而简单的解决方案:

data.Tasks.AddRange(
    data.Task.AsEnumerable().Select(t => new Task{
        creator_id   = t.ID,
        start_date   = t.Incident.DateOpened,
        end_date     = t.Incident.DateCLosed,
        product_code = t.Incident.ProductCode
        // so on...
    })
);
data.SaveChanges();

注意: 这个解决方案只适用于Task类上有一个导航属性(外键)(这里称为“Incident”)。 如果你没有,你可以使用“AsQueryable()”其他发布的解决方案之一。

这里有一种不声明额外类的方法:

public List<Product> GetProducts(int categoryID)
{
    var query = from p in db.Products
            where p.CategoryID == categoryID
            select new { Name = p.Name };
    var products = query.ToList().Select(r => new Product
    {
        Name = r.Name;
    }).ToList();

    return products;
}

但是,只有当您想将多个实体组合到一个实体中时,才可以使用这种方法。上面的功能(简单的产品到产品映射)是这样完成的:

public List<Product> GetProducts(int categoryID)
{
    var query = from p in db.Products
            where p.CategoryID == categoryID
            select p;
    var products = query.ToList();

    return products;
}

如果你正在使用实体框架,那么尝试从DbContext中删除属性,它使用你的复杂模型作为实体 当映射多个模型到名为实体的视图模型时,我也有同样的问题

public DbSet<Entity> Entities { get; set; }

从DbContext中删除条目修复了我的错误。