我有一个WCF方法,在两个表之间进行连接。现在我遇到了这个错误:
LINQ到实体不识别方法'Int32 Parse(System.String)‘方法,并且该方法不能转换为存储表达式。
在使用断点查看时,我在操作契约中创建的变量在运行代码时显示了undefined。
经营合同
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);服务方法
public List<Represetative> GetRepByCompA(string CompanyID)
{
try
{
TruckDb db = new TruckDb();
List<Represetative> RepList = new List<Represetative>();
var join = from t in db.Companies
join p in db.Represetatives on t.Id equals p.CompanyId
where t.Id == int.Parse(CompanyID) *//<-Shows CompanyID is Undefined-*
select new { t, p };
foreach (var item in join)
{
Represetative ph = new Represetative();
//REPRESETATIVES
ph.Name = item.p.Name;
ph.Email = item.p.Email;
ph.ContactNumber = item.p.ContactNumber;
ph.Quotes = item.p.Quotes;
ph.CompanyId = item.p.Id;
//REPRESETATIVES
//COMPANY
ph.Id = item.t.Id;
//COMPANY
RepList.Add(ph);
}
return RepList;
}
catch (Exception)
{
throw;
}
}当我试图将CompanyID更改为int时,在运行服务时会出现以下错误:
合同'GetRepByCompA‘中的操作'ITruckService’有一个名为'CompanyID‘的路径变量,它没有类型'string’。UriTemplate路径段的变量必须具有“string”类型。
发布于 2015-12-08 08:07:07
有四个问题。
NotSupportedException,这就是为什么要获得NotSupportedException。为了避免这种情况,您必须在LINQ表达式之前将CustomerId从string转换为int。Undefined字符串传递为CompanyID。显然,这不能被解析为整数。我想,Undefined的意思是“任何”。因此,如果where是Undefined,则需要丢弃Undefined条件。db.Companies的注意事项,但公司Id,它已经在db.Represetatives中表示为CompanyId。你根本不需要加入。db.Represetatives中的这类项与Represetative不同,您可以直接选择Represetative,而无需将中间投影到匿名类型并手动填充列表。您的方法可以重写如下:
// this assumes, that "RepresetativeEntityType" is a type name of representative entity
IQueryable<RepresetativeEntityType> representatives = db.Represetatives;
// pre-filter representatives, if CompanyID could be parsed
int companyIdToFilter;
if (int.TryParse(CompanyID, out companyIdToFilter))
{
representatives = representatives
.Where(_ => _.CompanyId == companyIdToFilter);
}
return representatives
.Select(_ => new Represetative
{
//REPRESETATIVES
Id = _.Id,
Name = _.Name,
Email = _.Email,
ContactNumber = _.ContactNumber,
Quotes = _.Quotes,
//COMPANY
CompanyId = _.CompanyId
})
.ToList();注意,如果db.Represetatives中的项类型是Represetative,甚至不需要投影,只需删除Select并调用ToList即可。
发布于 2015-12-08 07:02:52
替换此字符串:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);对此:
[WebGet(UriTemplate="/GetRepByCompA?CompanyID={CompanyID}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);并在Linq操作之前解析您的CompanyID变量:
int cid = int.Parse(CompanyId);
var join = from t in db.Companies
join p in db.Represetatives on t.Id equals p.CompanyId
where t.Id == cid
select new { t, p };https://stackoverflow.com/questions/34149809
复制相似问题