如何在IEnumerable Session属性中使用SingleOrDefault / where

时间:2013-04-17 05:02:39

标签: c# .net linq generics ienumerable

我正在使用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);

以上语法显示错误消息:

  

“无法从使用中推断出来。尝试指定类型参数   明确地”

上述语法不适用于属性TransactionItem2。为什么1,2,3不起作用?

1 个答案:

答案 0 :(得分:0)

调用方法时必须明确指定TSource

var entity = _TransactionItem2.SingleOrDefault<EntityClass>(item => item.BOMDetailRecordID == childRecordID);