您的位置:首页 > 其它

最近使用LINQ遇到的故障

2011-04-22 22:46 155 查看
1.成员“YouXieKu.Models.Product.Published”不支持转换为 SQL

public partial class Product{
.......

public bool Published
{
get
{
return (this.ReleaseDate <= DateTime.Now && (this.ExpireDate > DateTime.Now||this.ExpireDate==null)&&this.Storage.Count>0&&this.Approved==true);

}
}

}
可能原因:在执行LINQ查询时不该使用分部类中新增的属性值Published,直接通过原始值this.ReleaseDate <= DateTime.Now
&& (this.ExpireDate >
DateTime.Now||this.ExpireDate==null)&&this.Storage.Count>0&&this.Approved==true去判断

提示:由于以前的函数求值超时,函数求值被禁用。必须继续执行才能重新启用函数求值

可能原因:这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,
如果某个属性的get中做了很复杂的操作(而不是简单地返回一个私有变量的话),就有可能超过这个时间限制(如果strPage很大的话,你的正则运算就
很可能会超时)。

可以禁用自动求值的功能:

工具 -> 选项 -> 调试 -> 常规 -> 启用属性求值和其他隐式函数调用

去掉前面打勾,不过这样就不会对属性变量自动求值了。

3.LINQ语句 运行的时候提示"不支持带有本地集合的查询"

List<Department> _departments = dataContext.Department.GetAllChildDepartments(department);

var _ProductUnderDepartment =
(from a in dataContext.Product
let departmentIds = from b in _departments
select b.DepartmentID
where departmentIds.Contains(a.ProductID) == true && a.ReleaseDate <= DateTime.Now && (a.ExpireDate > DateTime.Now || a.ExpireDate == null) && a.Storage.Count > 0 && a.Approved == true
select a).ToList();

不使用let departmentIds = from b in _departments select b.DepartmentID 来动态从本地集合 _department 获取一组查询

改用直接先从_departments获取一组查询 List<int> _departments = dataContext.Department.GetAllChildDepartments(department).Select(p=>p.DepartmentID).ToList();

var ProductUnderDepartment =
(from a in dataContext.Product
where _departments.Contains(a.ProductID) == true && a.ReleaseDate <= DateTime.Now && (a.ExpireDate > DateTime.Now || a.ExpireDate == null) && a.Storage.Count > 0 && a.Approved == true
select a).ToList();
var _ProductUnderDepartment = (from b in ProductUnderDepartment
let s = from c in b.Storage
select c.UnitsInStock
where s != null && s.Sum() > 0
select b).ToList();
return _ProductUnderDepartment;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: