简体   繁体   中英

Delete a sql row using Linq in C#

I am trying to delete a sql table record from C# thru Linq but some reason DeleteonSubmit is not recoganized,am not sure what iam missing here, please guide me the right way

here is my code

          proxy = new ServiceClient("basicHttp_IService");
        //GatewayService.ServiceClient proxy = new ServiceClient("basicHttp_IService");

        SearchCriteria criteria = new SearchCriteria();
        criteria.UserRoles = new string[] { "*" };

        var stories = proxy.GetStoryItemsByCriteria(criteria);
        var programs = proxy.GetPrograms();
        var Profiles = proxy.GetProfiles();
foreach(StoryProgram sp in lstStoriestoClose)
{
    try
    {
        DateTime LastUpdateTimestamp;
        DateTime CurrentTime = DateTime.Now;
        LastUpdateTimestamp = sp.Story.LastUpdatedOn;

        if ((CurrentTime - LastUpdateTimestamp).TotalHours > 24)
        {
            //Delete the story from database
            var storytoDelete = from story in stories
                                where story.Id == sp.Story.Id
                                select story;

            //I am trying to delete the record like below
            stories.DeleteonSubmit(storytoDelete);
            stories.SubmitChanges();
            //Intellisense doesn't show the option DeleteonSubmit and SubmitChanges
        }
    }
}

Please guide me the right way to delete the record in SQL thru Linq

DeleteOnSubmit is for single entities. Your query returns a collection of entities (granted there may be only one entry, but it's still a collection). You can either use DeleteAllOnSubmit :

//Delete the story from database
var storytoDelete = from story in stories
                    where story.Id == sp.Story.Id
                    select story;

//I am trying to delete the record like below
stories.DeleteAllOnSubmit(storytoDelete);

or explicitly extract one record:

//Delete the story from database
var storytoDelete = from story in stories
                    where story.Id == sp.Story.Id
                    select story;

//I am trying to delete the record like below
stories.DeleteOnSubmit(storytoDelete.Single());  // or First, depending on whether you expect more than one match

It looks like your service is returning an array and not a valid linq database object. This is why it does not recognize the methods you expect to see. You need to examine the type and go from there.

You can always right click the service reference and configure to check/set the return type.

I just added delete functionality in WCF service and pass the sql record details to delete the record from SQL that solved my problem.

Thanks all for the suggestion and advise.

 foreach(StoryProgram sp in lstStoriestoClose)
        {
            try
            {
                DateTime LastUpdateTimestamp;
                DateTime CurrentTime = DateTime.Now;
                LastUpdateTimestamp = sp.Story.LastUpdatedOn;
                if ((CurrentTime - LastUpdateTimestamp).TotalHours > 24)
                {
                    //Delete the story from database
                    //Check the gateway to delete the record in the db.
                    var storytoDelete= from story in stories
                                        where story.Id== sp.Story.Id
                                        select story;

                   // stories.DeleteAllonSubmit(storytoDelete);
                    List<StoryProgram> lstStoriestoDelete = (from story in storytoDelete
                                                            join program in programs on story.ProgramId equals program.Id
                                                            join profile in Profiles on story.ProfileId equals profile.Id
                                                            select new StoryProgram(story, program, profile)).ToList();
                    foreach (StoryProgram sps in lstStoriestoDelete)
                    {
                        try
                        {
                            proxy.DeleteStoryItem(sps.Story.Id);
                        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM