相关疑难解决方法(0)

在LINQ语句中使用部分类属性

我试图找出做我认为很容易的最佳方法.我有一个名为Line的数据库模型,它代表发票中的一行.

看起来大致如此:

public partial class Line 
{
    public Int32 Id { get; set; }
    public Invoice Invoice { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public Decimal Price { get; set; }
    public Int32 Quantity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

此类是从db模型生成的.
我有另一个类,增加了一个属性:

public partial class Line
{
    public Decimal Total
    {
        get
        {
            return this.Price * this.Quantity
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,从我的客户控制器,我想做这样的事情:

var invoices = ( from c in _repository.Customers
                         where …
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc entity-framework asp.net-mvc-3

22
推荐指数
2
解决办法
8260
查看次数

在尝试解析列以进行不等式比较时,LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法

我的页面中有以下代码:

var myVar= Entity.SetName
                 .Where(p => int.Parse(p.ID) >= start &&
                  int.Parse(p.ID) <= end);
Run Code Online (Sandbox Code Playgroud)

start和end是int,但p.ID是string.所以我应该将p.ID转换为int.但我得到以下错误:

LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且此方法无法转换为存储表达式.

问题出在哪儿??

c# entity-framework

22
推荐指数
2
解决办法
4万
查看次数

在EF 4.0中将字符串转换为Int

有没有办法做到这一点?我在DB中有一个字符串字段,我想在我的LINQ查询中将其解析为一个int属性(是的,它必须在IQueryable级别,而不是在内存中).

我知道2年前EF 1.0无法做到这一点(即使LINQ to SQL支持开箱即用的这个基本功能)...但我只是想知道是否有人在这一点上提出了这样做的方法?

自定义函数映射?特殊语法?什么都没有....

更新:

我尝试了一个模型定义函数如下:

    <Function Name="ConvertToInt32" ReturnType="Edm.Int32">
      <Parameter Name="v" Type="Edm.String" />
      <DefiningExpression>
        CAST(v AS INT)
      </DefiningExpression>
    </Function>

    [EdmFunction("Model.Repository", "ConvertToInt32")]
    public static int ConvertToInt32(string value)
    {
        throw new InvalidOperationException("Only valid when used as part of a LINQ query.");
    }
Run Code Online (Sandbox Code Playgroud)

但它似乎没有用.我得到运行时异常:

        ErrorDescription=Type 'INT' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly.
        StackTrace:
             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeName(Node typeName, SemanticResolver sr)
             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeExprArgs(BuiltInExpr astBuiltInExpr, SemanticResolver sr)
             at System.Data.Common.EntitySql.SemanticAnalyzer.<CreateBuiltInExprConverter>b__73(BuiltInExpr bltInExpr, SemanticResolver sr) …
Run Code Online (Sandbox Code Playgroud)

linq linq-to-entities entity-framework entity-framework-4

20
推荐指数
1
解决办法
1万
查看次数

LINQ to Entities无法识别方法'Double Parse(System.String)'方法,并且此方法无法转换为存储表达式

我尝试运行报告时收到错误.问题出在这里:model.Referring = Math.Round(_newSurveyResult.Select(m => string.IsNullOrEmpty(m.Question1) ? 0 : Double.Parse(m.Question1)).Average());

public class SummaryDetails
{
    public int ChannelId { get; set; }
    public int ChannelGroupId { get; set; }
    public string Question1 { get; set; }
    public string Question2 { get; set; }
    public string Question3 { get; set; }
    public string Question4 { get; set; }
    public int OrganizationId { get; set; }
}

public ActionResult AreaManager(AreaManagerModel model)
{
    model.ShowCustomerReport = false;
    model.ShowSurveyReport = true;
    LoadModelVariablesonPostBack(model, 8);
    var _newSurveyResult = …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework

17
推荐指数
1
解决办法
2万
查看次数

linq to entities无法识别方法

我有这些方法:

   public int count(
        Guid companyId, Expression<Func<T, bool>> isMatch)
    {
        var filters = new Expression<Func<T, bool>>[]{
            x => x.PriceDefinition.CompanyId == companyId,
            isMatch
        };

        return GetCount(filters);
    }

public virtual int GetCount(
            IEnumerable<Expression<Func<T, bool>>> filters)
        {
            IQueryable<T> _query = ObjectSet;

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    _query = _query.Where(filter);
                }
            }

           return _query.Count();
        }
Run Code Online (Sandbox Code Playgroud)

使用时:

count(some_guid, x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId));
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

LINQ to Entities does not recognize the method 'Boolean IsMatch(System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64])' method, …
Run Code Online (Sandbox Code Playgroud)

.net c# linq-to-entities entity-framework

10
推荐指数
1
解决办法
9453
查看次数

Sql Server Freetext通过Entity Framework

我有一个使用ASP.NET MVC 3和Entity Framework 4查询Sql Server 2008数据库的现有网站.它包含一个包含大约10个字段的搜索表单,当用户单击提交按钮时,我动态创建一个仅包含指定搜索字段的Entity SQL请求,省略空字段.有用.到现在为止还挺好.

现在,客户端需要其中一个字段的全文搜索行为.我认为这个请求非常复杂,因为(AFAIK):

  • 实体框架本身不支持全文搜索
  • 我想避免存储过程来包装FTS语法,因为到目前为止我只使用"静态"SP,将逻辑保留在.NET代码中.所以我想尽量避免在过程中构建查询.并且每个可能的搜索字段组合创建一个过程不是一种选择.

到目前为止我能想到的解决方案:

  • 将存储过程或用户定义的函数作为seach preadicate放在WHERE子句中(虽然我不确定是否可行)
  • 仅在临时表中获取FTS结果,并​​在该临时表上执行其他过滤器.如果这项技术有很多FTS结果,我担心表现不佳......

用这种方法感觉最好的方法是什么?

.net sql-server full-text-search entity-framework entity-framework-4

10
推荐指数
1
解决办法
2820
查看次数

将linq扩展到实体以识别自定义方法

正如这里所讨论的以及无数更多:我想知道是否有任何方法可以继承/扩展/ ...实体框架4.1将自定义方法转换为SQL.

.net c# linq entity-framework entity-framework-4.1

7
推荐指数
1
解决办法
1214
查看次数