English 中文(简体)
利用电离板搜寻空地
原标题:use query linq in search with empty field

i have table with ten field . i need search query in LINQ that does this searching. my field is :

FirstName (string) , LastName (string),神父Name (string), NotationId (int), DebtPrice (int), BranchName (string), DebtId (int), MeliCode (string)

my problem is : when user don t fill field , this field shouldn t use in query search
thanks.

收集资料是合法的。

最佳回答

您可使用该法典:

var query = from d in datacontext.sample where   
                            (TBoxFName.Text=="" || d.FirstName.Contains(TBoxFName.Text.Trim()))
                            &&(TBoxLName.Text == "" || d.LastName.Contains(TBoxLName.Text.Trim()))
                            &&(TBoxFatherName.Text == "" || d.FatherName.Contains(TBoxFatherName.Text.Trim()))
                            && (TBoxPriceDebt.Text == "" || d.DebtPrice.ToString().Contains(TBoxPriceDebt.Text.Trim()))
                            && (CBoxBranch.Text == "" || d.BranchName.Contains(CBoxBranch.Text.Trim()))
                            &&(TBoxDebt.Text == "" || d.DebtId.Contains(TBoxDebt.Text.Trim()))
                            &&(TBoxMeliCode.Text == "" || d.MeliCode.Contains(TBoxMeliCode.Text.Trim()))
                                select d;
问题回答

I think (it is unclear) you mean something like:

IQueryable<YourType> query = /* some basic query; maybe db.TheTable */

if(!string.IsNullOrEmpty(firstName))
    query = query.Where(row => row.FirstName == firstName);
if(!string.IsNullOrEmpty(lastName))
    query = query.Where(row => row.LastName == lastName);
if(!string.IsNullOrEmpty(fatherName))
    query = query.Where(row => row.FatherName == fatherName);
// etc

var matches = query.Take(50).ToList();

this uses query composition to issue the most appropriate underlying query it can; for example, if this is LINQ-to-SQL and firstName and fatherName are supplied, you would get something like:

select top 50 {some columns} from [dbo].[TheTable] t
where t.FirstName = @p0 and t.FatherName = @p1

<代码>@p0和@p1均为数值参数。

你可以尝试写:

IEnumerable<YourType> query = //data taken from database
var queryWhere = query 
    .Where(x => x.FirstName  == varFirstName   || string.IsNullOrEmpty(x.FirstName ))
    .Where(x => x.LastName == varLastName      || string.IsNullOrEmpty(x.LastName ))
    .Where(x => x.FatherName == varFatherName  || string.IsNullOrEmpty(x.FatherName ))
    //...and so on... 
    .ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签