Вопрос

I have an IEnumerable list to which I want to prevent the addition of a duplicate item (an item already existing in the list) from my listbox. I have found that I must use LINQ but since I am new with ASP.NET. I didn't know how to compare my list item with the listbox items while adding a new one to my list:

This is how my list is displayed ::

   public ActionResult Liste()
   {
     try
     {
        List<RubriquepointageVM> lstVM = ServiceApplicatif.GetListe()
                                                          .OrderBy(x => x.Rubrique_PointageId)
                                                          .ToList();

        //  Drop Down List des listRubrique

        IEnumerable<RubriqueVM> listRubrique = RefDataManager.GetRefData<RubriqueVM>() as IEnumerable<RubriqueVM>;
        if (listRubrique.Any())
        {
            ViewBag.idrub = listRubrique.First().RubriqueId;
        }

        ViewData["CodeRubrique"] = new SelectList(listRubrique, "RubriqueId", "CODELIBELLE");

        return View(lstVM);
    }

    catch (Exception ex)
    {
        LoggerRubriquepointage.Error(string.Format("Exception : {0}", ex.Message.ToString()));
        throw new Exception("Erreur lors du chargement.");
    }
}

What I want to know is how to use LINQ to prevent the addition of an existing item in my Rubriqueintermediarelist from my listbox.

my Save() methode :

public JsonResult Save([DataSourceRequest] DataSourceRequest dsRequest, RubriquepointageVM vm)
    {
        try
        {
            IEnumerable<RubriqueVM> listRubrique = RefDataManager.GetRefData<RubriqueVM>() as IEnumerable<RubriqueVM>;
            ViewData["CodeRubrique"] = new SelectList(RefDataManager.GetRefData<RubriqueVM>(), "RubriqueId", "CODELIBELLE", 976);

            List<RubriquepointageVM> lstRubriqueinter = ServiceApplicatif.GetListe();

            **//Iwant my test to be her before adding an item**

           RubriquepointageVM rub = ServiceApplicatif.Save(vm); 


        DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID);
            dataCache.DropDataCache<RubriquepointageVM>();

            return Json(new[] { lstVMrub }.ToDataSourceResult(dsRequest, ModelState));

        }


        catch (Exception ex)
        {
            LoggerRubriquepointage.Error(string.Format("Exception : {0}", ex.Message.ToString()));
            throw new Exception("Erreur lors de l'enregistrement.");


        }
Это было полезно?

Решение

As I said in my comments, I cannot see exactly what you are trying to do but from what I gather I would try something like:

    if(! lstRubriqueinter.Any(x => x.RubriqueId == vm.RubriqueId))
    {
           RubriquepointageVM rub = ServiceApplicatif.Save(vm); 
        // you may need to add this rub to the list at this point. You will  have a better idea.
    }

I haven't tested this code because I don't have much to go on really from your question.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top