【问题标题】:NotSupportedException was caughtNotSupportedException 被捕获
【发布时间】:2026-02-01 05:30:01
【问题描述】:

我有一个 WCF 方法,用于在两个表之间进行连接。现在我遇到了这个错误:

LINQ to Entities 无法识别方法“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 时,我在运行服务时收到此错误:

合约“ITruckService”中的操作“GetRepByCompA”有一个名为“CompanyID”的路径变量,它没有“字符串”类型。 ÿ UriTemplate 路径段的变量必须具有“字符串”类型。

【问题讨论】:

  • 您不能将 CompanyID 作为整数而不是字符串传递吗?
  • 不,这就是我在底部提到的它说它需要是一个字符串值? @dimitarie
  • 从外部将 id 转换为 intvar id = int.Parse(CompanyID); var join = from ... where where t.Id == id ...;
  • 它给了我这个错误:Input string was not in a correct format.@Dennis
  • @RGdent:我想,CompanyId 不包含整数。你能在解析之前在调试器中查看CompanyId吗?

标签: c# .net entity-framework linq wcf


【解决方案1】:

有四个问题。

  1. EF LINQ 提供程序无法将每个有效的 C# 表达式转换为 SQL,这就是您收到 NotSupportedException 的原因。为避免这种情况,您必须在 LINQ 表达式之前将 CustomerIdstring 转换为 int

  2. 根据您的 cmets,可以选择将 Undefined 字符串作为 CompanyID 传递。显然,这不能解析为整数。我假设Undefined 的意思是“任何”。因此,如果CompanyIDUndefined,则需要丢弃where 条件。

  3. 1234563你根本不需要加入。
  4. 假设db.RepresetativesRepresetative中的项目类型不同,则可以直接选择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

【讨论】:

  • 未定义的值来自我在应用程序下拉列表中的选择更改值。@Dennis
  • 没关系,抱歉,您的解决方案修复了这个错误,谢谢
【解决方案2】:

替换这个字符串:

[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 };

【讨论】:

  • 为什么要改成WebGet? @龙舌兰酒
  • 首先,当您通过 GET Http Verb 声明您的操作合同时,这就像一个好习惯。
  • 同时拥有两个 Uri 模板会导致重复错误:'UriTemplate' duplicate named attribute argument @Tequila
  • 这是因为您的界面中有类似的 UriTemplate。只需使用唯一名称重命名 UriTemplate。例如,uriTemplate="/GetRepByCompany?CompanyID={CompanyID}", ...
  • 我做了,但错误仍然存​​在@Tequila 你介意解释一下为什么你使用两个 UriTemplates 也许如果我理解得更好我可以解决它吗?