Home > Net >  How to get the entire list instead of only the first result? Net Core 3.1
How to get the entire list instead of only the first result? Net Core 3.1

Time:07-22

The following functions return only the first row of the database, would you please guide me get the entire rows please?

My Function :

        private List<Phrases_Clp> PhrasesStringToListPhrases_Clp(string phrases_codes)
        {
            List<Phrases_Clp> phrases_Clps = new List<Phrases_Clp>();
            if (!String.IsNullOrEmpty(phrases_codes))
            {
                if (phrases_codes.Contains(","))
                {
                    string[] listprhases = phrases_codes.Split(",");
                    foreach (var item in listprhases)
                    {
                        var mentiontoinsert = _context.Phrases_Clps.FirstOrDefault(m => 
                        m.Phrase_Numero == item);
                        phrases_Clps.Add(mentiontoinsert);
                    }
                }
                else
                {
                    var mentiontoinsert = _context.Phrases_Clps.FirstOrDefault(m => 
                    m.Phrase_Numero == phrases_codes);
                    phrases_Clps.Add(mentiontoinsert);
                }

            }
            return phrases_Clps;
        }

UPDATE :

It still gives me the first result. I wonder if I should do changes in my view as I display information from another table which correspond the cell values in the current table.

Table Identification_Produit Table Identification_Produit

Table Phrases Clp Table_Phrase_Clp

Current Output:

Current Output

Desired Output is to display 'Mentions Dangers' of each Product.

GET:

       //GET: EvaluationRisque/Create
       [Authorize(Roles = "Administrateur, Modificateur")]
        public async Task<IActionResult> Create()
        {
            List<Models.dbo.equipements_protection.Equipement_Protection> listsequipement = _context.Equipement_Protections.ToList();
            Create_Viewbags();

            var model = new EvaluationRisquesViewModel();

            var produitUtiliseByProduitId = await _context.Identification_Produit.FirstOrDefaultAsync();

            model.MentionsDanger = produitUtiliseByProduitId;

            model.List_Mentions_Danger = PhrasesStringToListPhrases_Clp(model.MentionsDanger.Mentions_Danger);

            return View(model);
        }

Bootstrap Model in the View Page:

<td>@foreach (var mention_danger in Model.List_Mentions_Danger)
     {
      <p> @mention_danger.Phrase_Numero : @mention_danger.Phrase_Libelle 
     </p>} 
</td>

CodePudding user response:

Change FirstOrDefault(...) to .Where(...).ToList() (or use phrases_Clps.AddRange( _context.Phrases_Clps.Where(...))).

But actually there is no need to make multiple requests to the database and you can skip conditional splitting, so something like following should work:

List<Phrases_Clp> phrases_Clps = null;
if (!string.IsNullOrEmpty(phrases_codes))
{
   string[] listprhases = phrases_codes.Split(","); // will be array with one item if there are no commas in string
   phrases_Clps = _context.Phrases_Clps
      .Where(m => listprhases.Contains(m.Phrase_Numero))
      .ToList();
}
return phrases_Clps ?? new List<Phrases_Clp>();

CodePudding user response:

Because you are using FirstOrDefault function in this line:

 var mentiontoinsert = _context.Phrases_Clps.FirstOrDefault(m => m.Phrase_Numero == item);

replace it with Where function.

CodePudding user response:

Thinking that to utilize the query as IQueryable.

Updated:

Thanks for @GuruStron (Guru/Sensei)'s opinion and for pointing out the issue from my previous answer.

In short,

  1. Exit the function with an empty List if phrases_codes is an empty string or null.

  2. Working with IQueryable to prepare the different search criteria and defer the execution.

  3. Once the query is prepared, execute the query via .ToList().

if (String.IsNullOrEmpty(phrases_codes))
    return new List<Phrases_Clp>();

IQueryable<Phrases_Clp> query = _context.Phrases_Clps;

if (phrases_codes.Contains(","))
{
    string[] listprhases = phrases_codes.Split(",");

    query = query
        .Where(x => listprhases.Contains(x.Phrase_Numero));
}
else
{   
    query = query
        .Where(x => x.Phrase_Numero == phrases_codes);
}

return query.ToList();
  • Related