我正在使用 VS2010 Entity Framework 在会话中存储一个值。我使用下面的语法:
private IEnumerable _TransactionItem2
{
get
{
var msg = HttpContext.Current.Session["SESSION_MESSAGES_NAME"] as IEnumerable;
if (msg == null)
{
msg = new BOMDetailController().GetSearchData(0, 0);
HttpContext.Current.Session["SESSION_MESSAGES_NAME"] = msg;
}
return msg;
}
set
{
Session[_MenuName + "TransactionItem"] = value;
}
}
会话初始化方法如下:
public IEnumerable GetSearchData(int companyID = 0, long bOMID = 0)
{
var itemBrand = (from p in this.controllerCMN.Context.CmnItemBrands where p.IsDeleted == false select p).AsEnumerable();
var itemColor = (from p in this.controllerCMN.Context.CmnItemColors where p.IsDeleted == false select p).AsEnumerable();
var itemMaster = (from p in this.controllerCMN.Context.CmnItemMasters where p.IsDeleted == false select p).AsEnumerable();
var itemSpecificationMaster = (from p in this.controllerCMN.Context.CmnItemSpecificationMasters where p.IsDeleted == false select p).AsEnumerable();
var itemStyleMaster = (from p in this.controllerCMN.Context.CmnItemStyleMasters where p.IsDeleted == false select p).AsEnumerable();
var uOM = (from p in this.controllerCMN.Context.CmnUOMs where p.IsDeleted == false select p).AsEnumerable();
var bnOMMaster = (from p in this.Context.PlnBOMMasters where p.IsDeleted == false && p.CompanyID == companyID select p).AsEnumerable();
var prdPhase = (from p in this.Context.PlnPrdPhases where p.IsDeleted == false && p.ComapnyID == companyID select p).AsEnumerable();
var prdTask = (from p in this.Context.PlnPrdTasks where p.IsDeleted == false && p.ComapnyID == companyID select p).AsEnumerable();
var bOMDetail = (from p in this.Context.PlnBOMDetails where p.IsDeleted == false && p.CompanyID == companyID select p).AsEnumerable();
var query = from p in bOMDetail
select new
{
BOMDetailRecordID = p.BOMDetailRecordID,
CustomCode = p.CustomCode,
BOMID = p.BOMID,
BOMName = (from q in bnOMMaster where q.BOMID == p.BOMID select q.Description).FirstOrDefault(),
PhaseID = p.PhaseID,
PhaseName = (from q in prdPhase where q.PhaseID == p.PhaseID select q.PhaseName).FirstOrDefault(),
ItemID = p.ItemID,
ItemName = (from q in itemMaster where q.ItemID == p.ItemID select q.ItemName).FirstOrDefault(),
ColorID = p.ColorID,
ColorName = (from q in itemColor where q.ColorID == p.ColorID select q.ColorName).FirstOrDefault(),
StyleID = p.StyleID,
StyleName = (from q in itemStyleMaster where q.StyleID == p.StyleID select q.StyleName).FirstOrDefault(),
ItemSpecificationID = p.ItemSpecificationID,
ItemSpecificationName = (from q in itemSpecificationMaster where q.ItemSpecificationID == p.ItemSpecificationID select q.Description).FirstOrDefault(),
ItemBrandID = p.ItemBrandID,
BrandName = (from q in itemBrand where q.ItemBrandID == p.ItemBrandID select q.ItemBrandName).FirstOrDefault(),
UOMID = p.UOMID,
UOMName = (from q in uOM where q.UOMID == p.UOMID select q.UOMName).FirstOrDefault(),
ItemQty = p.ItemQty,
UnitPrice = p.UnitPrice,
StatusID = p.StatusID,
};
return query.WhereIf(bOMID != 0, p => p.BOMID == bOMID).OrderByDescending(w => w.BOMDetailRecordID);
}
我想查询上面的属性TransactionItem2
。
1) 我想计算房产上有多少物品TransactionItem2
`TransactionItem2.AsQueryable().Count()`
2)我想这样查询:
var entity = _ TransactionItem2.SingleOrDefault(item => item.BOMDetailRecordID == childRecordID);
3)我想使用查询
var entity = _ TransactionItem2.Where(item => item.BOMDetailRecordID == childRecordID);
我的扩展方法是
public static TSource Single<TSource>(this IEnumerable source)
{
return source.Cast<TSource>().Single();
}
public static TSource Single<TSource>(this IEnumerable source, Func<TSource, bool> predicate)
{
return source.Cast<TSource>().Single(predicate);
}
public static TSource SingleOrDefault<TSource>(this IEnumerable source)
{
return source.Cast<TSource>().SingleOrDefault();
}
public static TSource SingleOrDefault<TSource>(this IEnumerable source, Func<TSource, bool> predicate)
{
return source.Cast<TSource>().SingleOrDefault(predicate);
}
工作语法:
var entity = _TransactionItem2.SingleOrDefault(item => item.BOMDetailRecordID == childRecordID);
上面的语法显示了一条错误消息:
“不能从用法中推断出来。尝试显式指定类型参数”</p>
上述语法不适用于该属性TransactionItem2
。为什么 1,2,3 不起作用?