我有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;
}
}
}
GCT1015
相关分类