linq动态查询不填充集合

时间:2011-04-08 21:05:35

标签: silverlight linq ria linqpad

private void OnSearchExecute(IQueryable<SurveySearchCriteria> query)
    {
        SurveySearchCriteria ssc=query.Where(a => a.CriteriaString == "LastName").SingleOrDefault();

        string format = ssc.WhereClause;
        string args= string.Format(".{0}(\"{1}\")", SelectedSearchType, EnteredSearchCriteria);

        string returnval = string.Format(format, args);           




        ReadmissionTrackingApplication.Server.Services.SurveysDomainContext _context =
                                new ReadmissionTrackingApplication.Server.Services.SurveysDomainContext();

        EntityQuery<ReadmitPatientList> eq = _context.GetReadmitPatientListByCriteriaQuery(returnval);

       var lo= _context.Load<ReadmitPatientList>(eq);

使用此代码时,它不会填充我的内容。 我有一种感觉,因为我没有使用回调,但查询在linqpad中工作得很好。

returnval =“PatientLastName.Contains(\”test \“)”当我将其插入linqpad时,它可以正常工作。

ReadmitPatientList.AsQueryable()。如果( “PatientLastName.Contains(\” 测试\ “)”)

这是域名服务查询...

public IQueryable<Data.ReadmitPatientList> GetReadmitPatientListByCriteria(string formattedCriteriaString)
        {
            var query = this.ObjectContext.ReadmitPatientList.AsQueryable()
                             .Where(a => a.OriginalAdmitDate >= new DateTime(2010, 01, 01))
                             .Where(formattedCriteriaString);

            return query;

        }

我的问题是为什么这在linqpad中有效但在我的代码中没有。我正在使用silverlight,因此可能是因为没有回调。这是我对webbasede编程的第一次重大尝试,所以我仍然习惯了所有复杂的...

1 个答案:

答案 0 :(得分:0)

这是一个回调问题。我重构了我的代码,以便它实现回调功能并清理Domainservice调用,现在它可以工作。

客户服务:

public void getReadmitPatientList(System.Action<ICollection<ReadmitPatientList>> callback, string searchCriteria)
        {
            var q = _context.GetReadmitPatientListByCriteriaQuery(searchCriteria);
            _context.Load<ReadmitPatientList>(q, OnReamitPatientListLoaded, callback);
        }

        public void getReadmitPatientList(System.Action<ICollection<ReadmitPatientList>> callback, int days)
        {
            DateTime CurrentDate = DateTime.Today;

            DateTime entryDate = CurrentDate.AddDays(-days);

            var q = _context.GetReadmitPatientListQuery().Where(a => a.OriginalAdmitDate >= entryDate);
            _context.Load<ReadmitPatientList>(q, OnReamitPatientListLoaded, callback);

        }

        public void OnReamitPatientListLoaded(LoadOperation<ReadmitPatientList> lo)
        {
            if (!lo.HasError)
            {
                ICollection<ReadmitPatientList> q = new ObservableCollection<ReadmitPatientList>(lo.Entities);
                var action = (Action<ICollection<ReadmitPatientList>>)lo.UserState;
                action.Invoke(q);
            }
            else
            {
                MessageBox.Show(string.Format("{1}{0}{2}", Environment.NewLine, lo.Error.Message, lo.Error.InnerException));

                lo.MarkErrorAsHandled();
            }
        }

DOMAIN SERVICE:

 public IQueryable<Data.ReadmitPatientList> GetReadmitPatientListByCriteria(string formattedCriteriaString)
    {
        var query = this.ObjectContext.ReadmitPatientList.AsQueryable()
                         .Where(a => a.OriginalAdmitDate >= new DateTime(2010, 01, 01))
                         .Where(formattedCriteriaString);

        return query;

    }