我试图找出做我认为很容易的最佳方法.我有一个名为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) 我的页面中有以下代码:
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)'方法,并且此方法无法转换为存储表达式.
问题出在哪儿??
有没有办法做到这一点?我在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) 我尝试运行报告时收到错误.问题出在这里: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) 我有这些方法:
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) 我有一个使用ASP.NET MVC 3和Entity Framework 4查询Sql Server 2008数据库的现有网站.它包含一个包含大约10个字段的搜索表单,当用户单击提交按钮时,我动态创建一个仅包含指定搜索字段的Entity SQL请求,省略空字段.有用.到现在为止还挺好.
现在,客户端需要其中一个字段的全文搜索行为.我认为这个请求非常复杂,因为(AFAIK):
到目前为止我能想到的解决方案:
用这种方法感觉最好的方法是什么?
.net sql-server full-text-search entity-framework entity-framework-4
正如这里所讨论的以及无数更多:我想知道是否有任何方法可以继承/扩展/ ...实体框架4.1将自定义方法转换为SQL.