StartLinqDataContext con = new StartLinqDataContext();
                var q=con.FeeMain;
                if (cmb_sf.Text != "")
                    q = q.Where(w => w.wtdw == cmb_sf.Text);                   
                if (txt_blno.Text != "")
                    q = q.Where(w => w.blno == txt_blno.Text);                   
                if (usetime.Checked)
                    q = q.Where(w => w.indatatime>= Convert.ToDateTime(dtp1.Value)&&w.indatatime<=  Convert.ToDateTime(dtp2.Value));        
                if (cmb_ywlx.Text != "")
                    q = q.Where(w => w.receive == cmb_ywlx.Text);   

解决方案 »

  1.   

    不行的  报错  错误 1 无法将类型“System.Linq.IQueryable<TestLinqToSql.FeeMain>”隐式转换为“System.Data.Linq.Table<TestLinqToSql.FeeMain>”。存在一个显式转换(是否缺少强制转换?) D:\用户目录\Documents\Visual Studio 2008\Projects\TestLinqToSql\TestLinqToSql\Test.cs 62 21 TestLinqToSql
    而且要是多条件的话 这个也不适用吧
      

  2.   

    StartLinqDataContext con = new StartLinqDataContext();
                    var q=from c in con.FeeMain
                          select c;
                    if (cmb_sf.Text != "")
                        {q = from h in q 
                            where h.wtdw == cmb_sf.Text 
                            select h;}                   
                    if (txt_blno.Text != "")
                       {q = from h in q 
                            where h.blno == txt_blno.Text 
                            select h;} 
                    if (usetime.Checked)
                       {q = from h in q 
                            where h.indatatime >= Convert.ToDateTime(dtp1.Value)&& h.indatatime<=  Convert.ToDateTime(dtp2.Value));        
                           select h;}        
                    if (cmb_ywlx.Text != "")
                       {q = from h in q 
                            where h.receive == cmb_ywlx.Text 
                            select h;} 
      

  3.   

    解决了…… 一共有两种方法,效率由于数据量比较少,还没测出结果来,不过都能解决问题 //第一种方法
                DataClasses3DataContext con = new DataClasses3DataContext();
                
                var qu = from c in con.FeeMain select c;
                
                if (textBox1.Text != "")
                    qu = qu.Where(c => c.wtdw == textBox1.Text);
                if (textBox2.Text != "")
                    qu = qu.Where(c => c.vessel == textBox2.Text);            dataGridView1.DataSource = qu; //第二种方法
                DataClasses3DataContext con = new DataClasses3DataContext();
                var searchPredicate1 = PredicateExtensions.True<FeeMain>();
                if (textBox1.Text != "")
                    searchPredicate1 = searchPredicate1.And(c => c.wtdw == textBox1.Text);
                if (textBox2.Text != "")
                    searchPredicate1 = searchPredicate1.And(c => c.vessel == textBox2.Text);            var a = from c in con.FeeMain.Where(searchPredicate1) select c;
                dataGridView1.DataSource = a;
    原来在 执行 dataGridView1.DataSource = a; 这句代码的时候才会去查询数据库? 经过测试
    在第一种方法,在尚未执行到条件的时候,数据库跟踪里没有产生相应的跟踪信息,所以不会出现
    “先把数据全部查询出来再进行筛选这种情况”
    哦了……
      

  4.   

    忘记了 上面第二种方法中还有一个类  可以直接拿来用的,不用修改任何东西,网上查询的,说是老外写的,佩服……  
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Linq.Expressions;namespace TestLinqToSql
    {
       public static class PredicateExtensions
        {
            public static Expression<Func<T, bool>> True<T>() { return f => true; }
            public static Expression<Func<T, bool>> False<T>() { return f => false; }
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
            {
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
            }
            public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
            {
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
            }
        }
    }
      

  5.   

    楼主能分享最终的解决办法,赞一个。对后来遇到此问题的人是一种帮助:)你也可以参考这个:http://blog.csdn.net/q107770540/article/details/5724013
      

  6.   

    LINQ的延迟查询特性决定了,只有需要用到结果中的数据时,程序才会去执行查询
    //所以下边的where拼接有多少个,都只会在最后一步时才会去执行查询
    var qu = from c in con.FeeMain select c;
                
                if (textBox1.Text != "")
                    qu = qu.Where(c => c.wtdw == textBox1.Text);
                if (textBox2.Text != "")
                    qu = qu.Where(c => c.vessel == textBox2.Text);            dataGridView1.DataSource = qu;