首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NotSupportedException被抓了

NotSupportedException被抓了
EN

Stack Overflow用户
提问于 2015-12-08 06:58:27
回答 2查看 151关注 0票数 0

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

LINQ到实体不识别方法'Int32 Parse(System.String)‘方法,并且该方法不能转换为存储表达式。

在使用断点查看时,我在操作契约中创建的变量在运行代码时显示了undefined

经营合同

代码语言:javascript
复制
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);

服务方法

代码语言:javascript
复制
  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”类型。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-08 08:07:07

有四个问题。

  1. every不能将所有有效的C#表达式转换为NotSupportedException,这就是为什么要获得NotSupportedException。为了避免这种情况,您必须在LINQ表达式之前将CustomerIdstring转换为int
  2. 根据您的注释,可以选择将Undefined字符串传递为CompanyID。显然,这不能被解析为整数。我想,Undefined的意思是“任何”。因此,如果whereUndefined,则需要丢弃Undefined条件。
  3. 您的投影使用来自db.Companies的注意事项,但公司Id,它已经在db.Represetatives中表示为CompanyId。你根本不需要加入。
  4. 假设db.Represetatives中的这类项与Represetative不同,您可以直接选择Represetative,而无需将中间投影到匿名类型并手动填充列表。

您的方法可以重写如下:

代码语言:javascript
复制
// 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即可。

票数 0
EN

Stack Overflow用户

发布于 2015-12-08 07:02:52

替换此字符串:

代码语言:javascript
复制
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);

对此:

代码语言:javascript
复制
[WebGet(UriTemplate="/GetRepByCompA?CompanyID={CompanyID}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);

并在Linq操作之前解析您的CompanyID变量:

代码语言:javascript
复制
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 };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34149809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档