I got this code although it works fine but its giving me a warning(possible unintended reference comparison) on the code (st.staff_name == chk). I puzzled why this is. Help would be appreciated. Thank you.
private void buttonCreateSubmit_Click(object sender, EventArgs e)
{
Body body = new Body
{
body_content = richTextBoxBody.Text
};
tnDBase.AddToBodies(body);
tnDBase.SaveChanges();
var genid = tnDBase.Genres.Single(g => g.genre_name == comboBoxGenre.Text);
Article article = new Article()
{
article_name = textBoxTitle.Text,
genre_id = genid.genre_id,
status_id = 3,
body_id = body.body_id
};
tnDBase.AddToArticles(article);
tnDBase.SaveChanges();
if (checkedListBoxWriter.CheckedItems.Count != 0)
{
for (int x = 0; x <= checkedListBoxWriter.CheckedItems.Count - 1; x++)
{
var chk = checkedListBoxWriter.CheckedItems[x];
var staf = tnDBase.Staffs.SingleOrDefault(st => st.staff_name == chk);
WriterLog writerlog = new WriterLog()
{
article_id = article.article_id,
staff_id = staf.staff_id
};
tnDBase.AddToWriterLogs(writerlog);
tnDBase.SaveChanges();
}
}
}
You’re getting this warning because you’re comparing a
stringto anobject. Like all custom operators, the custom==operator for thestringtype (which compares the value of the string rather than whether or not the two strings have reference equality, which they might not) can only work when both operands arestringreferences.If you know that the items in
CheckedItemswill bestrings, then just cast it to a string: