【问题标题】:"DbComparisonExpression requires arguments with comparable type" error for no apparent reason! How do I fix it?“DbComparisonExpression 需要具有可比较类型的参数”错误,原因不明!我如何解决它?
【发布时间】:2014-02-06 15:33:50
【问题描述】:

我得到了一个

DbComparisonExpression requires arguments with comparable type

以下 LINQ to Entity 查询出错:

jobDetails.pdata = db.JobBoardUsers.Where(c => c.id.Equals(jbid)).Select(c => new JobBoardUserProfileModel() { ID = c.id, userid = c.userid, firstname = c.firstname, lastname = c.lastname, PostalCode = c.passcode, phone = c.phone, JobDescription = c.desiredjobtitle, resumetext = c.resumetext, savedresume = c.savedresume }).ToList();

除 ID (Int) 外,所有其他列都是 Varchar。在 EF(模型浏览器)中,ID 列显示为 Int32,其余为字符串。此外,该表确实有一个主键。

这是 JobBoardUserProfileModel 模型:

public class JobBoardUserProfileModel
{
    public int ID { get; set; }
    public string userid { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string PostalCode { get; set; }
    public string phone { get; set; }
    public string JobDescription { get; set; }
    public string savedresume { get; set; }
    public string resumetext { get; set; }
}

似乎一切都完美对齐。为什么会出现此错误?

【问题讨论】:

  • jbid 的类型是什么?为什么不使用c.id == jbid?
  • 谢谢 Habib,你找到了我的问题。我什至没有将 jbid 视为可能的问题,但确实它是一个字符串,并且应该针对“userid”而不是“id”进行评估!

标签: c# linq entity-framework visual-studio-2012


【解决方案1】:

我会尝试在您的 Linq 语句中使用 Typed ToList():

jobDetails.pdata = db........ToList<JobBoardUserProfileModel>()

如果没有它,它可能会返回一个无类型的 List 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多