投稿時間:2022-05-31 07:14:37 RSSフィード2022-05-31 07:00 分まとめ(21件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
Google カグア!Google Analytics 活用塾:事例や使い方 SNSの文脈に合わせた投稿をしよう https://www.kagua.biz/social/tiktok/20220531a1.html 違い 2022-05-30 21:00:10
Linux CentOSタグが付けられた新着投稿 - Qiita CentOSに最新のGitをインストールする https://qiita.com/hikotaro_san/items/0b5192f102b854bde41e centos 2022-05-31 06:38:19
Git Gitタグが付けられた新着投稿 - Qiita Gitを初期設定する https://qiita.com/hikotaro_san/items/ad895ab3ce469dffc0b3 configglobalusereamilhiko 2022-05-31 06:45:15
Git Gitタグが付けられた新着投稿 - Qiita CentOSに最新のGitをインストールする https://qiita.com/hikotaro_san/items/0b5192f102b854bde41e centos 2022-05-31 06:38:19
Ruby Railsタグが付けられた新着投稿 - Qiita パラメータを情報を指定する方法 https://qiita.com/yanatyamn/items/49e744ac9c4c57bd2387 rails 2022-05-31 06:53:19
海外TECH MakeUseOf How the Like and Share Buttons Ruined Social Media https://www.makeuseof.com/how-like-share-buttons-ruined-social-media/ content 2022-05-30 21:45:13
海外TECH MakeUseOf How Hard Is It to Create a Cryptocurrency? https://www.makeuseof.com/how-hard-is-it-to-create-a-cryptocurrency/ cryptocurrency 2022-05-30 21:30:14
海外TECH DEV Community Conditionally sorting an IQueryable https://dev.to/pbouillon/conditionally-sorting-an-iqueryable-2aig Conditionally sorting an IQueryableA couple of days ago I faced a situation where an incoming request may require its result to be sorted The model was similar to the following one public record Sorting string PropertyName string Order public class MyRequest IRequest lt SomeDto gt Some fields public Sorting Sort get init A few additional rules where implicit Sorting PropertyName was the name of one of the property of SomeDto the equivalent of a nameof on them Sorting Order would be either asc or desc Using EF Core conditionally sorting the resulting request has been easy but making it cleaner wasn t so much I finally came to a solution that I think is clean enough and wanted to share it with whom it may help someday ContextOur example will be based on a very simple model with only two fields and the same kind of sorting DTO I had public record User string FirstName string LastName public record Sorting string PropertyName string Order In our example Sorting will not be null but in a real case don t forget to verify it As for our data they may be something like a list or a DbSet with a query containing the incoming strings var users new List lt User gt new Pierre Bouillon new Thomas Anderson new Tom Scott new Keanu Reeves new Edward Snowden Or an EF Core query var users Context Users Where user gt var sort new Sorting FirstName asc We re all set Naive approachFirstly we can easily write a very naive approach testing each field that might require sorting and then the order before returning the sorted query if sort PropertyName nameof User FirstName if sort Order asc return users OrderBy user gt user FirstName else return users OrderByDescending user gt user FirstName if sort PropertyName nameof User LastName if sort Order asc return users OrderBy user gt user LastName else return users OrderByDescending user gt user LastName throw new ArgumentException That s a bit verbose especially checking each time for the order when there is only two values either asc or something else we could do better Using ternariesThe ternary operator also referred as conditional operator in the C reference is not always easy to read and should be employed with care but I find it pretty explicit there Let s refine our checking on the order with it if sort PropertyName nameof User FirstName return sort Order asc users OrderBy user gt user FirstName users OrderByDescending user gt user FirstName if sort PropertyName nameof User LastName return sort Order asc users OrderBy user gt user LastName users OrderByDescending user gt user LastName throw new ArgumentException That s a little bit better however we can notice a pattern there our code seems to be a repetition of the following structure if sort PropertyName a property return users sorted with the appropriate order There must be a way to directly return the appropriate request based on the PropertyName Using switch expressionIntroduced in C switch expressions might come handy in such cases Regarding the PropertyName we can apply our logic and directly return our ternary operator return sort PropertyName switch nameof User FirstName gt sort Order asc users OrderBy user gt user FirstName users OrderByDescending user gt user FirstName nameof User LastName gt sort Order asc users OrderBy user gt user FirstName users OrderByDescending user gt user FirstName gt throw new ArgumentException That s way more concise but now that the enclosing if statements have been refactored we can noticed that there is a lot of repeated code return sort PropertyName switch nameof property gt sort Order asc users OrderBy user gt user property users OrderByDescending user gt user property nameof property gt sort Order asc users OrderBy user gt user property users OrderByDescending user gt user property gt throw new ArgumentException We might want to look for another way of splitting our sorting Using expressionsTaking a step back we can see that sorting our users require us to extract two information The property nameThe orderIn our previous logic the evaluation of the order has been done within the evaluation of the property name and it led to code duplication at this place If we take a close look at the Enumerable OrderBy method we can see that it is taking the key as a function as parameter Let s once again refine our code so that we extract the key before applying the order Expression lt Func lt User string gt gt sortBy sort PropertyName switch nameof User FirstName gt user gt user FirstName nameof User LastName gt user gt user LastName gt throw new ArgumentException return sort Order asc users OrderBy sortBy users OrderByDescending sortBy That s way better Beside we just have to add another arm to the switch statement if we wanted to sort on a new property which would be fairly simple On a side note we were just comparing the order to asc without any other verification In your code you might want to take a closer look at the best way to test this value to handle cases and locals Happy coding 2022-05-30 21:15:19
金融 ニュース - 保険市場TIMES 損保ジャパン、北海道警察と「安全・安心な北海道の実現に向けた連携協定」締結 https://www.hokende.com/news/blog/entry/2022/05/31/070000 損保ジャパン、北海道警察と「安全・安心な北海道の実現に向けた連携協定」締結年月日に締結を発表損害保険ジャパン株式会社以下、損保ジャパンは、北海道警察と「安全・安心な北海道の実現に向けた連携協定」を締結したと、年月日に発表した。 2022-05-31 07:00:00
ニュース BBC News - Home French Open 2022: Daniil Medvedev and Stefanos Tsitsipas suffer shock exits at Roland Garros https://www.bbc.co.uk/sport/tennis/61636645?at_medium=RSS&at_campaign=KARANGA French Open Daniil Medvedev and Stefanos Tsitsipas suffer shock exits at Roland GarrosDaniil Medvedev and Stefanos Tsitsipas suffer surprise fourth round exits on a day of shocks in the men s draw at the French Open 2022-05-30 21:51:06
ニュース BBC News - Home Andy Murray criticises decision to remove ranking points from Wimbledon after easy Surbiton win https://www.bbc.co.uk/sport/tennis/61638782?at_medium=RSS&at_campaign=KARANGA Andy Murray criticises decision to remove ranking points from Wimbledon after easy Surbiton winAndy Murray says the ATP and WTA have not made a great move by taking away Wimbledon s ranking points this year 2022-05-30 21:09:41
ニュース BBC News - Home French Open: Iga Swiatek beats Zheng Qinwen and meets Jessica Pegula next in Paris https://www.bbc.co.uk/sport/tennis/61635018?at_medium=RSS&at_campaign=KARANGA French Open Iga Swiatek beats Zheng Qinwen and meets Jessica Pegula next in ParisTop seed Iga Swiatek fights back from dropping her first set in matches to beat China s Zheng Qinwen and reach the French Open quarter finals 2022-05-30 21:45:20
ビジネス ダイヤモンド・オンライン - 新着記事 ロシア領内攻撃できるロケット砲、「ウクライナに提供せず」 米大統領 - WSJ発 https://diamond.jp/articles/-/304051 米大統領 2022-05-31 06:09:00
北海道 北海道新聞 首相直轄、感染症の司令塔 爆発的拡大に対処、概要判明 https://www.hokkaido-np.co.jp/article/687515/ 首相 2022-05-31 06:27:00
北海道 北海道新聞 兵士返せとアゾフ連隊家族 ロシア支配地域で消息不明 https://www.hokkaido-np.co.jp/article/687514/ 連隊 2022-05-31 06:17:00
北海道 北海道新聞 チャドで衝突140人死傷 金鉱労働者間 https://www.hokkaido-np.co.jp/article/687513/ 近く 2022-05-31 06:07:00
北海道 北海道新聞 コウノトリ4羽ふ化 島根・雲南で6年連続 https://www.hokkaido-np.co.jp/article/687512/ 島根県雲南市 2022-05-31 06:07:00
ビジネス 東洋経済オンライン ホンダが発売予告「ZR-V」とはどんな車なのか? ヴェゼル以上、CR-V未満のサイズで年内発売 | 新車レポート | 東洋経済オンライン https://toyokeizai.net/articles/-/592637?utm_source=rss&utm_medium=http&utm_campaign=link_back 東洋経済オンライン 2022-05-31 06:30:00
ニュース THE BRIDGE シンガポールの妊活スタートアップHannah Life Technologies、プレシリーズAで515万米ドルを調達 https://thebridge.jp/2022/05/hannah-life-pre-series-a-funding-pickupnews シンガポールの妊活スタートアップHannahLifeTechnologies、プレシリーズAで万米ドルを調達YCombinatorbackedSGfertilitystartupbagsmfundingUpdated重要なポイント妊娠を希望するカップルのためのスタートアップHannahLifeTechnologiesは日、プレシリーズAラウンドで万米ドルを資金調達したと発表した。 2022-05-30 21:45:47
ニュース THE BRIDGE 目指せ年商2億!民族ハッピー組がトークン販売完了【フィナンシェ放送局 #8】 https://thebridge.jp/2022/05/happy-people-party-ends-token-sales-financiepodcast 目指せ年商億民族ハッピー組がトークン販売完了【フィナンシェ放送局】本稿はトークンを使ったクラウドファンディング「フィナンシェ」が配信するポッドキャスト「フィナンシェ放送局」の記事からの転載第回放送ポッドキャストはこちら↓SpotifyApplePodcastフィナンシェ放送局はトークンを使ったクラウドファンディング「フィナンシェ」で巻き起こる、ファンとプロジェクトの話題をお届けするポッドキャストです。 2022-05-30 21:30:22
ニュース THE BRIDGE 全国80施設で遊び放題、レジャーのサブスク「レジャパス!」【Monthly Pitch!注目スタートアップ】 https://thebridge.jp/2022/05/mpweeklynews_origress_parks-cyberagentcapital-insight 全国施設で遊び放題、レジャーのサブスク「レジャパス」【MonthlyPitch注目スタートアップ】本稿はベンチャーキャピタル、サイバーエージェント・キャピタルが運営するサイトに掲載された記事からの転載サービス概要ORIGRESSPARKSオリグレスパークスは、年月に設立したテーマパークベンチャーです。 2022-05-30 21:15:43

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)