हमारे पास एक नया डेवलपर है जो अभी लिंककॉक्ल सीख रहा है। अपने कोड पर जाने के दौरान, हमने देखा कि वह मूल्य तुलना के लिए ==
के बजाय .Equals
का उपयोग कर रहा था। उस पर, वह की तुलना string
से कर रहा है। हमें पूरी तरह से परेशान किया गया था, यह काम किया था!लिंक से एसक्यूएल - स्ट्रिंग से int की तुलना करते समय सटीक लौटाता है -> Id.Equals ("5") सत्य लौटाता है
कुछ परीक्षण करने के बाद यह केवल LinqToSql के साथ काम करता प्रतीत होता है। ऑब्जेक्ट्स के लिए लिंक false
लौटाता है और केवल int.Equals(“string”)
false
देता है।
Customer
Id(PK)(int)
Name(varchar)
CompanyId(FK)(int)
Company
Id(PK)(int)
Name(varchar)
Console.WriteLine("{0}.Equals(\"{1}\") returns {2}", 3, "3", 3.Equals("3"));
using (WrDataClassesDataContext wrDataContext = new WrDataClassesDataContext())
{
var customers1 = from c in wrDataContext.Customers
where c.CompanyId.Equals("3")
select c;
Console.WriteLine("int .Equals String Results: ");
foreach (Customer customer in customers1)
{
Console.WriteLine(customer.Name);
}
var customers3 = from c in wrDataContext.Customers
where c.CompanyId == 3
select c;
Console.WriteLine("int == int Results: ");
foreach (Customer customer in customers3)
{
Console.WriteLine(customer.Name);
}
}
परिणाम::
3.Equals("3") returns False
int .Equals String Results:
Mike
Joe
Candy
int == int Results:
Mike
Joe
Candy
Press any key to continue . . .
किसी भी स्पष्टीकरण
नीचे दो एसक्यूएल तालिकाओं का उपयोग एक साधारण परीक्षण है?
LinqToSql में == बनाम .quals() के उपयोग के बारे में एक महत्वपूर्ण पक्ष नोट, http://stackoverflow.com/questions/8105732/ef-4-1-linq-to-sql-what-is-better उपयोग करना-बराबर होती है-या –