Search code examples
asp.netlinq-to-sqldevexpress

Custom Deleting in LinqToSqlDataSource


i have a GridView bound to a SqlToLinqDataSource. In our software Entities are not going to get deleted physically, we only set a Column "deletedFlag" to true.

Now I want to get into the deleting-Event of the DataSource an make the Custom-Delete there trying tthis way:

protected void LinqServerModeDataSourceNEU_Deleting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e)
    {
        if(Verband.verbandKuerzel.ToLower() == "wsv")
        {
            using (wsv.wsv2DataContext context = new wsv.wsv2DataContext())
            {
                foreach ( DictionaryEntry item in e.Keys)
                {
                    wsv.mitgliedschaft temp = (wsv.mitgliedschaft)context.mitgliedschaft.Where(x => x.id == int.Parse(item.Value.ToString()));
                    temp.deletedFlag = 1;
                }
                context.SubmitChanges();
            }
        }
        else if(Verband.verbandKuerzel.ToLower() == "wsb")
        {
            using (wsb.wsb2DataContext context = new wsb.wsb2DataContext())
            {
                foreach (DictionaryEntry item in e.Keys)
                {
                    wsb.mitgliedschaft temp = (wsb.mitgliedschaft)context.mitgliedschaft.Where(x => x.id == int.Parse(item.Value.ToString()));
                    temp.deletedFlag = 1;
                }
                context.SubmitChanges();
            }
        }
    }

But I am getting this Error:

Das Objekt des Typs "System.Data.Linq.DataQuery`1[WebApplication_NEON.wsb.mitgliedschaft]" kann nicht in Typ "WebApplication_NEON.wsb.mitgliedschaft" umgewandelt werden.

Can somebody help me what I am doing wrong?


Solution

  • IEnumerable.Where() method returns an IEnumerable<T> object (a collection of objects).

    In your case you get an instance of IEnumerable<WebApplication_NEON.wsb.mitgliedschaft>, which doesn't have property deltedFlag, instead of an instance of WebApplication_NEON.wsb.mitgliedschaft.

    You need to access each row in database individually -> change Where to Single.

    protected void LinqServerModeDataSourceNEU_Deleting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e)
    {
        if(Verband.verbandKuerzel.ToLower() == "wsv")
        {
            using (wsv.wsv2DataContext context = new wsv.wsv2DataContext())
            {
                foreach ( DictionaryEntry item in e.Keys)
                {
                    /* vvvvv line bellow vvvvv */
                    wsv.mitgliedschaft temp = (wsv.mitgliedschaft)context.mitgliedschaft.Single(x => x.id == int.Parse(item.Value.ToString()));
                    temp.deletedFlag = 1;
                }
                context.SubmitChanges();
            }
        }
        else if(Verband.verbandKuerzel.ToLower() == "wsb")
        {
            using (wsb.wsb2DataContext context = new wsb.wsb2DataContext())
            {
                foreach (DictionaryEntry item in e.Keys)
                {
                /* vvvvv line bellow vvvvv */]
                    wsb.mitgliedschaft temp = (wsb.mitgliedschaft)context.mitgliedschaft.Single(x => x.id == int.Parse(item.Value.ToString()));
                    temp.deletedFlag = 1;
                }
                context.SubmitChanges();
            }
        }
    }