PDA

View Full Version : آموزش: ساخت فیلتر داینامیک با استفاده از دستورات linq



crazy_1892
جمعه 28 بهمن 1390, 23:07 عصر
سلام دوستان می خوام یک جستحو برای یک از جداول بذارم
جستجو بر اساس چند فیلد است



var _Bank = (from c in Contax.Banks
1-where c.Name.Contains(textBoxX1.Text)
2-where c.Shobeh.Contains(textBoxX1.Text)
3-where c.Ostan.Contains(textBoxX1.Text)
select new {c.Name,c.Shobeh,c.Ostan,c.City,c.Tel });


مثلا می خوام وفتی کاربر استان را انتخاب کرد فقط where برای استان اجرا شود

A.S.Roma
جمعه 28 بهمن 1390, 23:26 عصر
با استفاده از قابلیت Defered Execution در LINQ به شکل زیر می تونید به راحتی Linq Query تون رو بنویسید :


var _Bank = (from c in Contax.Banks
select c);
if(clause)
_bank = _bank.where(r=>r.Name.Contains(txt.Text));
if(clause2)
...

در پایان از _Bank اون چیزی رو که می خواهید Select بزنید.
فکر می کنم از طریق Expression Tree هم بشه نوشت !

مهدی کرامتی
شنبه 29 بهمن 1390, 11:20 صبح
string name = txtName.Text;
string shobeh = txtShobeh.Text;
string ostan = txtOstan.Text;
var _Bank = (from c in Contax.Banks
where (c.Name.Contains(name) || name == string.Empty)
&& (c.Shobeh.Contains(shobeh) || shobeh == string.Empty)
&& (c.Ostan.Contains(ostan) || ostan == string.Empty)
select new {c.Name,c.Shobeh,c.Ostan,c.City,c.Tel });

crazy_1892
شنبه 29 بهمن 1390, 13:46 عصر
string name = txtName.Text;
string shobeh = txtShobeh.Text;
string ostan = txtOstan.Text;
var _Bank = (from c in Contax.Banks
where (c.Name.Contains(name) || name == string.Empty)
&& (c.Shobeh.Contains(shobeh) || shobeh == string.Empty)
&& (c.Ostan.Contains(ostan) || ostan == string.Empty)
select new {c.Name,c.Shobeh,c.Ostan,c.City,c.Tel });


سلام ممنون
یک textbox بیشتر ندارم