發表文章

目前顯示的是有「Linq」標籤的文章

用於Linq Where的建置

 #region setting db.where             var pred = PredicateBuilder.New<class>(true);             if (!string.IsNullOrEmpty(model.COMPID))                 pred.And(p => p.col == model.col); #endregion db.where(pred);

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression.'

今天遇到了這個問題,原因是我在串Model中的某些資料的時候使用db.table.where()下判斷式的時候產生了以下的錯誤 System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression.' 這個錯誤的原因是我當初使用db.table.Where(d => d.col.Equals($"")); 因此改成 string strLinq = $""; db.table.Where(d => d.col.Equals(strLinq)); 這樣就會過了

Linq Join

Linq的Join 相當於 SQL的 Join 目前Linq有兩種方法 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(     this IEnumerable<TOuter> outer, //被Join的資料     IEnumerable<TInner> inner, //要Join的資料     Func<TOuter, TKey> outerKeySelector, //與inner相同的資料      Func<TInner, TKey> innerKeySelector, //與outer相同的資料     Func<TOuter, TInner, TResult> resultSelector); //select 後的資料 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(     this IEnumerable<TOuter> outer,     IEnumerable<TInner> inner,     Func<TOuter, TKey> outerKeySelector,     Func<TInner, TKey> innerKeySelector,     Func<TOuter, TInner, TResult> resultSelector,     IEqualityComparer<TKey> comparer); //客製比較器 from c in Customers join o in Orders on c.CustomerID equals o.CustomerID select c.ContactName 可以反寫成 Custo...

Entity Framework 自動生成 Model中

圖片
如何使用Entity Framework 製作 Model 首先我是有先安裝了 這三個NuGet的是要先下載的 接著要做的事情是 建立DB連線 檢視 => 其他視窗 => 伺服器總管 => 加入連線 輸入一些必要欄位之後按測試連線,通過即可 記得類型必須選第四張圖的類型 建立這個的目的就是在於要複製連線字串 接著打開Nuget 輸入 Scaffold-DbContext " 複製字串 " Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force 此憑證鏈結是由不受信任的授權單位發出的 有可能會遇到這個錯誤 原因是因為少加了 TrustServerCertificate=true; 大致上這樣就完成了 參考: https://ithelp.ithome.com.tw/articles/10262110 https://dotblogs.com.tw/mis2000lab/2021/11/29/SqlClient_SqlException_SSL_Provider_error#google_vignette

Linq Distinct不能使用問題

 Linq有一個功能是 .Distinct 可以去除重複項,但是直接使用的話貌似沒有什麼用。 比如一個資料表有 AAA , BBB , CCC , AAA , AAA , BBB , AAA , CCC 輸出之後還會是 AAA , BBB , CCC , AAA , AAA , BBB , AAA , CCC 這是因為並沒有做出一個比較表,因此 var ListData = new List<string>() {"AAA" , "BBB" , "CCC" ,"AAA" , "BBB" , "CCC" ,"AAA","AAA"  };      var distinctData = ListData.Distinct(new Compare()); class Compare: IEqualityComparer<string>{     public bool Equals(string x , string y){          return x.Equals(y);      }     public int GetHashCode(string obj){          return obj.GetHashCode();      } } 這樣的話就會變成 AAA BBB CCC了 但是這樣就只能適用於 string 假如丟 int 進去就會無法使用 public class PropertyComparer<T> : IequalityComparer<T>{     private PropertyInfo _PropertyInfo;     public PropertyComparer(string propertyName){          // get PropertyInfo type from T      ...