简体   繁体   English

无法使用实体框架从linq查询转换列表

[英]Cannot convert List from linq query using Entity Framework

I have a conversion error in my program. 我的程序中出现转换错误。 I use a method to query db (with Entity Framework) and get all the entries of a table Web_Groups_joint_Profils : 我使用一种方法(使用Entity Framework)查询db并获取表Web_Groups_joint_Profils所有条目:

public partial class Web_Group_joint_Profils
{
    [Key]
    [Column(Order = 0)]
    [StringLength(255)]
    public string GroupName { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int IDProfil { get; set; }
}

And here is the method to get all rows: 这是获取所有行的方法:

public List<Web_Group_joint_Profils> GetGroupAndId()
{
    var grpId = context.WebGroupProfil
               .Select(a => new 
               {
                   GroupName = a.GroupName,
                   IDProfil = a.IDProfil
               }).ToList();
    return grpId;
 }

But grpId is invalid : 但是grpId无效:

Impossible to implicitely convert type "System.Collections.Generic.List<> to System.Collections.Generic.List" 无法将类型“ System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List”

When I hover on the declaration ToList() it says 当我将鼠标悬停在声明ToList()它说

Creates a List<T> of an IEnumerable<out T>

So I have tried changing the return type 所以我尝试更改返回类型

List<IEnumerable<T>> 

without success. 没有成功。 I'm guessing my linq query creates an anonymous type I don't know how to handle. 我猜我的linq查询创建了一个我不知道如何处理的匿名类型。

The type of the method is List<Web_Group_joint_Profils> but you are returning a List of anonymous type. 该方法的类型为List<Web_Group_joint_Profils>但是您将返回匿名类型的List。 You can change it like this: 您可以这样更改:

.Select(a => new Web_Group_joint_Profils
{
    GroupName = a.GroupName,
    IDProfil = a.IDProfil
}).ToList();

And if you still get error probably it is because you cannot project onto a mapped entity then you need to create a DTO class with needed properties from the Web_Group_joint_Profils entity like this: 如果仍然出现错误,可能是因为您无法投影到映射的实体上,那么您需要从Web_Group_joint_Profils实体中创建具有所需属性的DTO类,如下所示:

public class TestDTO
{
    public string GroupName { get; set; }
    public string IDProfil { get; set; }
}

Then: 然后:

.Select(a => new TestDTO
{
    GroupName = a.GroupName,
    IDProfil = a.IDProfil
}).ToList();

The .Select() creates an object of an anonymous type, so the list will necessarily be the same anonymous type. .Select()创建一个匿名类型的对象,因此列表必然是相同的匿名类型。 This is not the same type as you are returning. 这与您返回的类型不同。

What you need the .Select() to do is create an object of the type you desire. 您需要.Select()进行的操作是创建所需类型的对象。 However, Entity Framework won't like that because it will try and convert that to SQL which it can't. 但是,Entity Framework不会喜欢它,因为它将尝试将其转换为它无法转换的SQL。

So, your code probably needs to be something like this: 因此,您的代码可能需要像这样:

public List<Web_Group_joint_Profils> GetGroupAndId()
{
    var grpId = context.WebGroupProfil
        .Select(a => new 
        {
            GroupName = a.GroupName,
            IDProfil = a.IDProfil
        })
        .AsEnumerable() // This forces EF to run the query and materialise the data as anon objects
        .Select(a=>new Web_Group_joint_Profils(a.GroupName, a.IDProfil)
        .ToList();
    return grpId;
}

I've assumed that Web_Group_joint_Profils has a constructor that takes the two arguments. 我假设Web_Group_joint_Profils具有一个接受两个参数的构造函数。 If not just modify the construction to fit your scenario. 如果不是,只需修改构造以适合您的方案。

Instead of creating a list of anonymous objects, in your LINQ select object initialiser create your required object: 无需创建匿名对象列表,而是在LINQ select对象初始化程序中创建所需的对象:

.Select(a => new Web_Group_joint_Profils
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM