无法使用Entities将IEnumerable类型隐式转换为IQueryable

时间:2021-10-02 16:35:05

I am completely new to Entities (I've taken over another persons project) and therefore I am prolly missing some key facts but I'll try and explain my problem:

我对实体完全不熟悉(我接管了另一个人项目),因此我遗漏了一些关键事实,但我会尝试解释我的问题:

I have applicants who has a table with their information like first name etc. And I also have a table with the ASP.NET Users. The tables are linked by the user_id.

我的申请人有一个表,上面有他们的信息,如名字等。我还有一个表与ASP.NET用户。表由user_id链接。

I am trying to make the login name a selectable listitem in a dropdown but in order to do so I need to join the two tables. I have written the following code:

我试图在下拉列表中将登录名设置为可选择的列表项,但为了这样做,我需要加入这两个表。我写了以下代码:

var container = new ModelContainer();
var manager = new ApplicantDBManager();

IEnumerable<Business.Entities.EF.Applicant> applicantQuery;

if (txtSearch.Text.Equals(string.Empty))
{
    applicantQuery = container.Applicants;
}
else
{
    var ids = manager.ApplicantSearchForIDs(ddlSearch.SelectedValue, txtSearch.Text, chkIncludeInactive.Checked);

    applicantQuery = (from a in container.Applicants
                      join u in container.Users on a.user_id equals u.user_id
                      where ids.Contains(a.user_id)
                      select new
                      {
                          user_id = a.user_id,
                          first_name = a.first_name,
                          last_name = a.last_name,
                          login_name = u.login_name,
                          date_of_birth = a.date_of_birth,
                          kot_cpr = a.kot_cpr,
                          address = a.address,
                          email = a.email,
                          telephone_number = a.telephone_number,
                          citizenship = a.citizenship
                      });
}

I am getting an error like "Cannot implicitly convert type System.Linq.Iqueryable to System.Collections.Generic.IEnumerable" and it seems not to matter how I try and fix it (I've tried adding .ToList().AsQueryable(), First() and such with no luck). I think it has something to do with the Applicant entity?

我收到一个错误,如“无法将类型System.Linq.Iqueryable隐式转换为System.Collections.Generic.IEnumerable”,似乎并不重要我如何尝试修复它(我尝试添加.ToList()。AsQueryable( ),First()等没有运气)。我认为这与申请人实体有关?

3 个解决方案

#1


5  

This is the problem:

这就是问题:

select new 
{
    ...
}

That's creating an instance of an anonymous type. How do you expect that to create a Business.Entities.EF.Applicant object?

那是创建一个匿名类型的实例。您如何期望创建Business.Entities.EF.Applicant对象?

You may well just need to change your code to:

您可能只需要将代码更改为:

select new Business.Entities.EF.Applicant
{
    ...
}

#2


0  

You need to change the code. Change the code to

您需要更改代码。将代码更改为

select new Business.Entities.EF.Applicant
{
user_id = a.user_id,
first_name = a.first_name,
last_name = a.last_name,
login_name = u.login_name,
date_of_birth = a.date_of_birth,
kot_cpr = a.kot_cpr,
address = a.address,
email = a.email,
telephone_number = a.telephone_number,
citizenship = a.citizenship
}

#3


0  

select new returns anonymous type object whereas applicantQuery is of type Business.Entities.EF.Applicant. So, like Jon Skeet said, return the object of the expected type.

选择new返回匿名类型对象,而applicantQuery的类型为Business.Entities.EF.Applicant。因此,像Jon Skeet所说,返回预期类型的​​对象。

#1


5  

This is the problem:

这就是问题:

select new 
{
    ...
}

That's creating an instance of an anonymous type. How do you expect that to create a Business.Entities.EF.Applicant object?

那是创建一个匿名类型的实例。您如何期望创建Business.Entities.EF.Applicant对象?

You may well just need to change your code to:

您可能只需要将代码更改为:

select new Business.Entities.EF.Applicant
{
    ...
}

#2


0  

You need to change the code. Change the code to

您需要更改代码。将代码更改为

select new Business.Entities.EF.Applicant
{
user_id = a.user_id,
first_name = a.first_name,
last_name = a.last_name,
login_name = u.login_name,
date_of_birth = a.date_of_birth,
kot_cpr = a.kot_cpr,
address = a.address,
email = a.email,
telephone_number = a.telephone_number,
citizenship = a.citizenship
}

#3


0  

select new returns anonymous type object whereas applicantQuery is of type Business.Entities.EF.Applicant. So, like Jon Skeet said, return the object of the expected type.

选择new返回匿名类型对象,而applicantQuery的类型为Business.Entities.EF.Applicant。因此,像Jon Skeet所说,返回预期类型的​​对象。