[英]Applying Substring in LINQ to SQL
List<DocumentDTO> lstDocs = objService
.SelectDocumentInfo()
.Where(l => int.Parse(l.FileName.Substring(0, l.FileName.IndexOf('\'))) = docID)
.ToList();
我的docID是一個int
值
279\Chrysanthemum.jpg
279是我的docID我想只獲得那些在指定FileName前面有279的記錄,或者如果docID更改為其他內容,那么只能通過LINQ獲取這些記錄。
我怎樣才能做到這一點?
這可能是一種可行的方法:
List<DocumentDTO> lstDocs = objService.SelectDocumentInfo()
.Where(l => l.FileName.StartsWith(string.Concat(docID, @"\"))
.ToList();
編輯
你也可以這樣做
List<DocumentDTO> lstDocs = objService
.SelectDocumentInfo()
.Where(l => Convert.ToInt32( (l.FileName.Split('\\'))[0] ) == docID)
.ToList();
通過對字符串應用split函數
你的代碼有一個變化
List<DocumentDTO> lstDocs = objService
.SelectDocumentInfo()
.Where(l => int.Parse(l.FileName.Substring(0, l.FileName.IndexOf('\'))) == docID)
.ToList();
你需要把“==”而不是“=”檢查上面的正確代碼
注意:如果你有像279 \\ Chrysanthemum.jpg那樣的字符串只有它像這樣的279 \\ Chrysanthemum1.jpg而不是這個失敗了
只需從這樣的字符串中獲取數字並嘗試
stringThatHaveCharacters = stringThatHaveCharacters.Trim();
Match m = Regex.Match(stringThatHaveCharacters, "\\d+");
int number = Convert.ToInt32(m.Value);
所以試試這樣吧
List<DocumentDTO> lstDocs = objService
.SelectDocumentInfo()
.Where(l => Convert.ToInt32( (Regex.Match(l.FileName, "\\d+")).Value) == docID)
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.