次のコードサンプルです:Linqクエリは「System.Object ....型の定数値を作成できません」という理由を繰り返します。なぜですか?
private void loadCustomer(int custIdToQuery)
{
var dbContext = new SampleDB();
try
{
var customerContext = from t in dbContext.tblCustomers // keeps throwing:
where t.CustID.Equals(custIdToQuery) // Unable to create a constant value of type 'System.Object'.
select new // Only primitive types ('such as Int32, String, and Guid')
{ // are supported in this context.
branchId = t.CustomerBranchID, //
branchName = t.BranchName //
}; //
if (customerContext.ToList().Count() < 1) //Already Tried customerContext.Any()
{
lstbCustomers.DataSource = customerContext;
lstbCustomers.DisplayMember = "branchName";
lstbCustomers.ValueMember = "branchId";
}
else
{
lstbCustomers.Items.Add("There are no branches defined for the selected customer.");
lstbCustomers.Refresh();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
dbContext.Dispose();
}
}
私は私が間違っているのかを理解することができません。私は続ける"タイプ 'System.Object'の定数値を作成できません。このコンテキストでは、プリミティブ型( 'Int32、String、およびGuid'など)のみがサポートされています。
感謝です:( – Neel
"t.CustID == custIdToQuery"と "t.CustID.Equals(custIdToQuery)"の違いを教えてください。事前に感謝しています – Neel
@ニール '=='と '.Equals()'の違いについてこの質問を見てください:http://stackoverflow.com/questions/814878/c-sharp-difference-between- and-equal – Alex