C# Visual Studio2019 にて、Linq のWhereの検索条件のプロパティー部分を動的に設定したいと考えておりますが、
式木を使って実現可能かと考えているのですが、使い方が良く分かりません。ご存知の方がおられましたら、宜しくお願いします。
下記コードのifでPv1~Pv21を判定している部分のPvを変数として与えたいと考えています。
・制作対象は、WPFとなります。開発環境は、Windows10、.Net Core3.1 となります。
宜しくお願いします。
namespace Test_Expression { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public string LabelText; private MainWindowViewModel _viewModel; public class SerchData { public string Pv1; public string Pv2; public string Pv3; public string Pv4; public string Pv5; public string Pv6; public string Pv7; public string Pv8; public string Pv9; public string Pv10; public string Pv11; public string Pv12; public string Pv13; public string Pv14; public string Pv15; public string Pv16; public string Pv17; public string Pv18; public string Pv19; public string Pv20; public string Pv21; } public MainWindow() { InitializeComponent(); DataContext = new { LabelText }; _viewModel = new MainWindowViewModel { LabelText = "検索文字A~Z" }; this.DataContext = _viewModel; } public class MainWindowViewModel : INotifyPropertyChanged { private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public void Button1_Click(object sender, RoutedEventArgs e) { List<SerchData> serchDatas = new List<SerchData>(); SerchData serchData = new SerchData(); serchData.Pv1 = "a"; serchData.Pv2 = "b"; serchData.Pv3 = "c"; serchData.Pv4 = "d"; serchData.Pv5 = "e"; serchData.Pv6 = "f"; serchData.Pv7 = "g"; serchData.Pv8 = "h"; serchData.Pv9 = "i"; serchData.Pv10 = "j"; serchData.Pv11 = "k"; serchData.Pv12 = "l"; serchData.Pv13 = "m"; serchData.Pv14 = "n"; serchData.Pv15 = "o"; serchData.Pv16 = "p"; serchData.Pv17 = "q"; serchData.Pv18 = "r"; serchData.Pv19 = "s"; serchData.Pv15 = "t"; serchData.Pv16 = "u"; serchData.Pv17 = "v"; serchData.Pv18 = "w"; serchData.Pv19 = "x"; serchData.Pv20 = "y"; serchData.Pv21 = "z"; serchDatas.Add(serchData); string al = "v"; if (serchDatas.Where(o => o.Pv1 == al).Count() >= 1) { _viewModel.LabelText = "Pv1"; } if (serchDatas.Where(o => o.Pv2 == al).Count() >= 1) { _viewModel.LabelText = "Pv2"; } if (serchDatas.Where(o => o.Pv3 == al).Count() >= 1) { _viewModel.LabelText = "Pv3"; } if (serchDatas.Where(o => o.Pv4 == al).Count() >= 1) { _viewModel.LabelText = "Pv4"; } if (serchDatas.Where(o => o.Pv5 == al).Count() >= 1) { _viewModel.LabelText = "Pv5"; } if (serchDatas.Where(o => o.Pv6 == al).Count() >= 1) { _viewModel.LabelText = "Pv6"; } if (serchDatas.Where(o => o.Pv7 == al).Count() >= 1) { _viewModel.LabelText = "Pv7"; } if (serchDatas.Where(o => o.Pv8 == al).Count() >= 1) { _viewModel.LabelText = "Pv8"; } if (serchDatas.Where(o => o.Pv9 == al).Count() >= 1) { _viewModel.LabelText = "Pv9"; } if (serchDatas.Where(o => o.Pv10 == al).Count() >= 1) { _viewModel.LabelText = "Pv10"; } if (serchDatas.Where(o => o.Pv11 == al).Count() >= 1) { _viewModel.LabelText = "Pv11"; } if (serchDatas.Where(o => o.Pv12 == al).Count() >= 1) { _viewModel.LabelText = "Pv12"; } if (serchDatas.Where(o => o.Pv13 == al).Count() >= 1) { _viewModel.LabelText = "Pv13"; } if (serchDatas.Where(o => o.Pv14 == al).Count() >= 1) { _viewModel.LabelText = "Pv14"; } if (serchDatas.Where(o => o.Pv15 == al).Count() >= 1) { _viewModel.LabelText = "Pv15"; } if (serchDatas.Where(o => o.Pv16 == al).Count() >= 1) { _viewModel.LabelText = "Pv16"; } if (serchDatas.Where(o => o.Pv17 == al).Count() >= 1) { _viewModel.LabelText = "Pv17"; } if (serchDatas.Where(o => o.Pv18 == al).Count() >= 1) { _viewModel.LabelText = "Pv18"; } if (serchDatas.Where(o => o.Pv19 == al).Count() >= 1) { _viewModel.LabelText = "Pv19"; } if (serchDatas.Where(o => o.Pv20 == al).Count() >= 1) { _viewModel.LabelText = "Pv20"; } if (serchDatas.Where(o => o.Pv21 == al).Count() >= 1) { _viewModel.LabelText = "Pv21"; } } } }
回答3件
あなたの回答
tips
プレビュー