Is this valid?

Trying to write a DALINQ expression where certain “where” parameters may or may not be needed or entered by the user. Is this valid? The “User*” vars are Boolean and indicate if the user has entered a value to used to query the table.

Is this proper LINQ to use in this situation?

  var iq := from irec in fDatamodule.DataAdapter.GetTable<lpv8oelib.Data.invhistd>
            where ((irec.ClassCode='8001') or (irec.ClassCode='8002') or (irec.ClassCode='8003') or (irec.ClassCode='8045'))
            where ((irec.InvoiceDate>=d1) and (irec.InvoiceDate<=d2))

            // conditional stuff???
            where ((UserPub) or (irec.PubNumber=PubText.Text.Trim))
            where ((UserClient) or (irec.ClientNum=ClientText.Text.Trim))
            where ((UserAE) or (irec.AENumber=AdExecText.Text.Trim)) 
            where ((UserClass) or (irec.Classcode=ClassText.Text.Trim))
            // end of conditional

            select new class(irec.MillerAd8,irec.AdYear4,irec.ClientNum,irec.ClientName,irec.ClassCode,irec.AENumber,irec.BSSCompany,irec.InvoiceNumber,
                             irec.InvoiceDate,irec.GrossAmount,irec.InvoiceType,irec.FirstInsDate,irec.LastInsDate,irec.PubName,irec.PubNumber);

Hello

Yes, it is valid. However the resulting SQL will contain unnecessary checks (as a result of User* vars conversion). If you want to get a more clean SQL or need to have more flexible control over the DA LINQ query you could use the following approach:

DateTime? date = ...;

var query = from x in this.fDataModule.DataAdapter.GetTable<Orders>() where x.Id > 0
		select x;

if (date.HasValue)
    query = query.Where(x => x.OrderDate <= date.Value.AddYears(-1));

var result = data.ToList();

Note how additional condition is added to the query if the nullable DateTime value is set.

Regards

Thanks! Much appreciated.