亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不同欄目的搜索方法

不同欄目的搜索方法

C#
江戶川亂折騰 2023-06-25 13:29:08
我有Form和ComboBox。TextBox第一個包含列名稱,第二個包含要搜索的文本。作為來源,ComboBox取自元素。該方法在按下按鈕的過程中被調用。ListTypeSearchItemSearchSearch()Search如果給該列一個這樣的名稱,則什么也找不到EF.Functions.Like(item.Value, ...); // Value = "FullName"如果指定模型中的列,則搜索有效EF.Functions.Like(w.FullName, ...); 是否可以在同一方法中替換應搜索的列Search()?ListTypeSearch.Add(new ItemSearch { Value = "FullName", Display = "some text" });ListTypeSearch.Add(new ItemSearch { Value = "PassportSeries", Display = "some text" });ListTypeSearch.Add(new ItemSearch { Value = "PassportNumber", Display = "some text" });public class ItemSearch{    public string Value { get; set; }    public string Display { get; set; }}internal List<WorkerTableRow> Search(ItemSearch item, string text){    try    {        Found = new List<WorkerTableRow>();        using (ModelContext model = new ModelContext())        {            Found = (from w in model.Workers                     where EF.Functions.Like(w.FullName, // this code                                             String.Format("%{0}%", text))                     select new WorkerTableRow                     {                         ...                     })                     .ToList();        }    }    catch (Exception ex) { ... }    return Found;}更新現在我確實喜歡這樣了。其作品。這可以簡化嗎?where EF.Functions.Like(w.GetProperty(item.Value),                         String.Format("%{0}%", text))public partial class Workers{    ...    public string FullName { get; set; }    public string PassportSeries { get; set; }    public string PassportNumber { get; set; }    public string GetProperty(string name)    {        switch (name)        {            case "FullName":                return FullName;            case "PassportSeries":                return PassportSeries;            case "PassportNumber":                return PassportNumber;            default:                return string.Empty;        }    }}
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

如果使用Like(w.GetProperty(item.Value), ...),則請求在客戶端上執行,而不是在服務器上執行。要將整個請求發送到服務器,您可以執行以下操作:

List<WorkerTableRow> Search(ItemSearch item, string text)

{

? ? string pattern = string.Format("%{0}%", text);


? ? using (var model = new ModelContext())

? ? {

? ? ? ? IQueryable<Worker> query = model.Workers;


? ? ? ? if (item.Value == "FullName")

? ? ? ? ? ? query = query.Where(w => EF.Functions.Like(w.FullName, pattern));


? ? ? ? if (item.Value == "PassportSeries")

? ? ? ? ? ? query = query.Where(w => EF.Functions.Like(w.PassportSeries, pattern));


? ? ? ? if (item.Value == "PassportNumber")

? ? ? ? ? ? query = query.Where(w => EF.Functions.Like(w.PassportNumber, pattern));


? ? ? ? return query.Select(w => new WorkerTableRow { ... }).ToList();

? ? }

}


查看完整回答
反對 回復 2023-06-25
  • 1 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號