[英]Linq query using a variable
嗨,我的項目中有以下Linq:
string claim;
claim = txtImageVerificationClaimSearch.Text;
var claimsearch = (from x in dbContext.view_ImageVerification_Shortened
where(x.intClaimID = claim)
select new
我收到一些錯誤消息:
Cannot implicitly convert type 'string' to 'int'
我知道錯誤的含義,但我不知道解決該錯誤的語法。 我也收到錯誤消息:
Error 2: Cannot convert lambda expression to type 'string' because it is not a delegate type
C:\_Applications-TFS\IVS\Main\ImageVerificationSystem\ImageVerificationSystem\Default.aspx.cs 97 36 ImageVerificationSystem
我也得到這個:
Delegate 'System.Func<ImageVerificationSystem.view_ImageVerification_Shortened,int,bool>' does not take 1 arguments
誰能告訴我我在做什么錯?
你的問題是, claim
是一個字符串,intClaimID是一個int。 換句話說,您正在比較蘋果和桔子。
您要么需要執行以下操作:
where x.ClaimName == claim
或者,您需要要求用戶輸入一個數字,在這種情況下,您必須將其轉換為ant int(從文本框的字符串中)
int userClaimID = int.Parse(claim); // or TryParse
然后將其添加到您的表情中
where x.intClaimID == userClaimID
您需要double =
進行比較。 另外,您正在嘗試將string
與int
進行比較。 我建議將claim
以int
第一。
int claimId = int.Parse(claim);
var claimsearch = (from x in dbContext.view_ImageVerification_Shortened
where(x.intClaimID == claimId)
select x);
int claimInt = Int.Parse(claim);
...
您不需要使用括號
from x in dbContext.view_ImageVerification_Shortened
where x.intClaimID.ToString() == claim
select new { .. }
另外==
用於比較=
用於分配。
如果要比較兩個不兼容的類型,你需要轉換them.In的一個這種情況下,無論是使用ToString
整數(intClaimID),或解析claim
到整數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.