سؤال

وايم تواجه مشاكل في بناء الاستعلام مع LINQ إلى SQL تعبير استعلام البيانات في C #.

ما أحاول أن يقوم في نهاية المطاف لا على هذا التعبير الزائفة التعليمات البرمجية.

public IQueryable<CTest> searchRecords(string category, string searchString, DateTime startDate, DateTime endDate, int searchType, int searchType2)
        {
            //-Search All Records
            //-From the information table
            //-By the category column containing a specific search
            //-Also by
            //  ~if both a startDate and endDate are entered (not (0000,00,00) OR null) then get records
            //  by this expression
            //  ~else then don't worry about this statement
            //-Also by
            //  ~if a searchType is equal zero(0) then search for records from the 
            //  search_type table equal to zero(0)
            //  ~if a searchType is equal one(1) then search for records from the 
            //  search_type table equal to one(1)
            //  ~else then don't worry about this statement
            //-Also by
            //  ~if a searchType2 is equal zero(0) then search for records from the 
            //  search_type table equal to zero(0)
            //  ~if a searchType2 is equal one(1) then search for records from the 
            //  search_type table equal to one(1)
            //  ~else then don't worry about this statement

            //Here is my attempt at it
            /*  
                var table = db.table1;

                switch (category)
                {
                case "_category1":
                    var records =
                        from c in table
                        where c.column1.ToString().Contains(searchString)
                        select new CTest
                        {
                            test_id = c.id,
                            test_name = c.name,
                            test_number = c.number,
                            date = ((startDate != null) && (endDate != null)) ? ((c.test_date >= startDate) && (c.test_date <= endDate)) :
                                         c.test_date),
                            test_type = (searchType == 0 ? (c.searchType = 0) :
                                        searchType == 1 ? (c.searchType = 1) :
                                        c.searchType),
                            test_type2 = (searchType2 == 0 ? (c.searchType2 = 0) :
                                        searchType2 == 1 ? (c.searchType2 = 1) :
                                        c.searchType2)
                        };
                    break;
                default:
                    break;
                }
             */
        }

وشكرا مقدما.

هل كانت مفيدة؟

المحلول

وهذا سيكون من الأسهل أن تفعل مع طرق الإرشاد من جملة LINQ:

 var records = context.Table
                      .Where( c => c.column1.Contains( searchString ) );
 if (startDate != null && endDate != null)
 {
     records = records.Where( c => c.test_date >= startDate
                                    && c.test_date <= endDate );
 }

 ...

وهذا سوف بناء التعبير LINQ كما تذهب على طول. تأخر التقييم حتى كنت في الواقع استدعاء بعض تمديد يتطلب التحديد إلى أن يؤديها، مثل عدد () أو ToList ()، أو تعداد عناصر النتيجة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top