0

I'm wondering why is it returning a null value in the code that i made below:

using (var context = new MusicStoreDBEntities())
{
    var bay = (from g in context.stringInstrumentItems
               where g.brand.name == name.Text select g)
               .FirstOrDefault();
    context.stringInstrumentItems.Remove(bay);
    context.SaveChanges();
}

The var bay is returning null. What have i done wrong? Here is the equivalent raw sql query that I'm trying to turn into an entity framework:

string queryGuitarItems = "DELETE S FROM stringInstrumentItem S JOIN brand B ON S.brandId = B.brandId WHERE B.name = @brand";
using (SqlConnection connectionGuitarItems = new SqlConnection(ConfigurationManager.ConnectionStrings["musicStoreConnection"].ToString()))
{
    using (SqlCommand commandGuitarItems = new SqlCommand(queryGuitarItems, connectionGuitarItems))
    {
        connectionGuitarItems.Open();
        commandGuitarItems.Connection = connectionGuitarItems;
        commandGuitarItems.Parameters.Add(new SqlParameter("@brand", name.Text));
        commandGuitarItems.ExecuteNonQuery();

        connectionGuitarItems.Close();
        commandGuitarItems.Parameters.Clear();

    }
}

Let me know if the two queries are similar or not. I'm really trying to change all my raw sql queries into entity framework and this is a start.

3
  • run sql-profiler and look ad generated sql-code Commented Jul 13, 2017 at 14:19
  • FirstOrDefault() returns null if nothing is found matching your conditions, are you sure that you have a matching item in your database? Commented Jul 13, 2017 at 14:20
  • That SQL command will happily "delete" 0 records. Your FirstOrDefault() may return null when no matching item is found. So check for that null. Commented Jul 13, 2017 at 14:21

1 Answer 1

1

You've not included your join in the linq query. Since you haven't included Brand, its not getting any records based on your where clause.

Since you're using entity framework, you might try something like this:

using System.Data.Entities;

using (var context = new MusicStoreDBEntities())
{
    var bay = context.stringInstrumentItems.Include(i => i.brand)
        .FirstOrDefault(x => x.brand.name == name.Text);

    if (bay != null)
    {
        context.stringInstrumentItems.Remove(bay);
        context.SaveChanges();
    }
}

The .Include() gets the related brand record from the database the same way the JOIN does in your SQL code there so you should have a record now.

The problem with excluding it is the same as if there's no matching records on either side of a join - no records will be selected by the query.

Sign up to request clarification or add additional context in comments.

4 Comments

as an experiment, put a breakpoint on the Remove line and inspect the element. Given your linq query as the selection, I'll bet anything that g.brand is null or empty
your solution is giving me an error in this part : (i => i.brand) .. it says cannot convert lambda expression to type 'string' because it is not a delegate type
Did you add the using for System.Data.Entities? If you're using Entity Framework 6 it should work fine. Otheriwse, replace that lambda expression with "brand" to target the linked entity
yea i just figure it out few seconds ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.