C#使用.Where()进行实体框架过滤
我使用实体框架在C#中工作,我正在尝试筛选联系人查询,以获取具有相同Id的所有联系人。我可以获取所有C#使用.Where()进行实体框架过滤,c#,entity-framework,filtering,C#,Entity Framework,Filtering,我使用实体框架在C#中工作,我正在尝试筛选联系人查询,以获取具有相同Id的所有联系人。我可以获取所有联系人,但使用Where进行筛选时遇到问题。我知道有些地方不对劲,但我不能很准确地指出,任何帮助都将不胜感激 见以下相关代码: public IEnumerable<model.Contact> Execute(GetContactById parameters) { IEnumerable<model.Contact> ContactsById = null;
联系人
,但使用Where
进行筛选时遇到问题。我知道有些地方不对劲,但我不能很准确地指出,任何帮助都将不胜感激
见以下相关代码:
public IEnumerable<model.Contact> Execute(GetContactById parameters)
{
IEnumerable<model.Contact> ContactsById = null;
DbRetryHandler.RetryHandler(delegate(DeviceModelContext retryContext)
{
ContactsById = retryContext.Contact
.Where(c => c.Id.equals(parameters.Id))
.Select(c => new model.Contact
{
// unrelated code
});
});
return ContactsById;
}
public IEnumerable执行(GetContactById参数)
{
IEnumerable ContactsById=null;
RetryHandler.RetryHandler(委托(DeviceModelContext retryContext)
{
ContactsById=retryContext.Contact
其中(c=>c.Id.equals(parameters.Id))
.选择(c=>新型号。联系
{
//无关代码
});
});
返回联系人bYID;
}
提供程序在识别无法转换为SQL的表达式时遇到问题。尽量简化表达式,以便更容易地将其转换为SQL
public IEnumerable<model.Contact> Execute(GetContactById parameters)
{
IEnumerable<model.Contact> ContactsById = null;
DbRetryHandler.RetryHandler(delegate(DeviceModelContext retryContext)
{
var parametersId = parameters.Id; // <-- store id in variable
camerasByDeviceId = retryContext.Contact
.Where(c => c.Id == parametersId) // <-- use == instead of Equals
.Select(c => new model.Camera
{
// unrelated code
});
});
return ContactsById;
}
public IEnumerable执行(GetContactById参数)
{
IEnumerable ContactsById=null;
RetryHandler.RetryHandler(委托(DeviceModelContext retryContext)
{
var parametersId=parameters.Id;//c.Id==parametersId)//new model.Camera
{
//无关代码
});
});
返回联系人bYID;
}
什么是参数.Id
数据类型?是否出现异常?您是否尝试使用=
而不是Equals
?@YacoubMassad是的,我尝试过,结果仍然为空。我可以看到,当我以断点运行此问题时,c.Id从未获得值,因此最终它只是将parameters.Id与Null进行比较。@Ian parameters.Id是Guid。@FrederikPetersen您不应该更改原始问题并使用有效的解决方案进行更新,因为在同一问题中运行的其他程序员无法跟踪错误原来。这就是答案所在。请将其恢复到原始状态(未运行的代码)