0

My list can insert 2 type of records into database: Type and Text. Now the records in my database is like:

ID   StoreID   Type        Text
1       1      Human        a
2       1      Human        b
3       1      Animal       c

How to combine texts and use comma separated if they have same type? I my database should look like this:

ID   StoreID   Type        Text
1       1      Human        a,b
2       1      Animal       c

Here's some of my codes for Respirators table:

public NPG_Chemical()
{
    this.NPG_Symptoms = new HashSet<NPG_Symptom>();
}
public virtual ICollection<NPG_Symptom> NPG_Symptoms { get; set; }
internal void CreateSymptom(int count = 1)
{
    for (int i = 0; i < count; i++)
    {
        NPG_Symptoms.Add(new NPG_Symptom());
    }
}

and in my Chemical controller, I have:

public ActionResult Create()
{
    var nPG_Chemical = new NPG_Chemical();
    nPG_Chemical.CreateSymptoms(1);
    return View(nPG_Chemical);
}
[HttpPost]
public ActionResult Create(NPG_Chemical nPG_Chemical)
{
    db.NPG_Chemical.Add(nPG_Chemical);
    db.SaveChanges();
    return Redirect("Create");
}

In Chemical.cshtml:

<p>
Symptom Type
</p>
<div id="humans">
<label>
    Human
</label>
@Html.EditorFor(model => model.NPG_Chemical_Symptoms)
</div>
@Html.AddLink("+", "#humans", ".symptom", "NPG_Chemical_Symptoms", typeof(NPG_Administrative_Utility.Models.NPG_Chemical_Symptom))
<div id="animals">
<label>
    Animal
</label>
</div>
@Html.AddLink("+", "#animals", ".symptom", "NPG_Chemical_Symptoms", typeof(NPG_Administrative_Utility.Models.NPG_Chemical_Symptom))

In Symptom page:

@model NPG_Administrative_Utility.Models.NPG_Chemical_Symptom

<div class="symptom" style="display:inline-block;">
<p>
    @Html.RemoveLink("x", "div.symptom", "input.mark-for-delete")
    @Html.HiddenFor(x => x.DeleteSymptom, new { @class = "mark-for-delete" })
    @Html.TextBoxFor(x => x.Symptom_Text)
    @Html.Hidden("Symptom_Type", "Human")
    &nbsp;&nbsp;&nbsp;
</p>
</div>

When I click on the "+", another div will be created. I also check if div id is "Animal", then the Symptom_Type will be changed to Animal. Now I have no problem to store all data into database, but the problem is how to combine records with same Symptom_Type?

Layla Wang
  • 65
  • 7
  • 1
    How to use a comma separated list? A bit confused. Do you have a list with 3 records and want to "convert" to a list showing 2 records? or ...? – Shane van Wyk Dec 10 '15 at 22:19
  • 1
    @ShaneVanWyk Yes, convert all records into 2 records. One is for Human, and another one is for Animal. – Layla Wang Dec 10 '15 at 22:22
  • 2
    Can you show some code? Do you have these data in objects of type `List` or the database? What data access technology are you using? entity framework? can you show your model classes? – Yacoub Massad Dec 10 '15 at 22:25
  • 1
    **Don't do what you are trying to do.** You are reducing details about the data. Instead, leave the table stored as the three rows and create a view that does the group by string join. You really don't want to be changing your data the way you are asking. – test Dec 10 '15 at 22:42
  • @YacoubMassad I use ICollection. Please see the updated code. – Layla Wang Dec 10 '15 at 22:43
  • @test I'm trying to find a way to combine data before store them into database. – Layla Wang Dec 10 '15 at 22:45
  • Maybe this will be of assistance to you , http://stackoverflow.com/questions/614542/use-linq-to-concatenate-multiple-rows-into-single-row-csv-property , You can group the list using Linq and create a new list to save to the database. – Shane van Wyk Dec 10 '15 at 22:46
  • Text should probably be stored in a separate table to support a 1:many relationship. Your main class then should have a `ICollection Texts` property. – Robert McKee Dec 10 '15 at 22:48
  • @ShaneVanWyk var SymptomType = nPG_Chemical.NPG_Chemical_Symptoms.GroupBy(Type => Type.Symptom_Type); foreach (var type in SymptomType) { System.Diagnostics.Debug.WriteLine(String.Join(", ", type.Select(x => x.Symptom_Text.ToString()).ToArray())); } Now I can get a string with ",", then how can I store this into Symptom table? Sorry, I'm new. – Layla Wang Dec 11 '15 at 01:44
  • @user3602167 - `var SymptomType ` will be of type IEnumerable which can be converted back to a list. So you can use 'Linq' to reassign the 'IEnumerable Collection' to the specified list, or however you used to save the items. Remove the `Debug.WriteLine` and and assign it to a new list then save that list. Hope that makes sense. The function I referenced before is simply just to reformat the list and write you the outputs, You will need to reassign those outputs to a list of some sort or save it to the database. – Shane van Wyk Dec 11 '15 at 01:58
  • @ShaneVanWyk var text = String.Join(", ", type.Select(x => x.Symptom_Text.ToString()).ToArray()); nPG_Chemical.NPG_Chemical_Symptoms.Add(new NPG_Chemical_Symptom { Symptom_Type = type.Key.ToString(), Symptom_Text = text }); Now I can add the new list into database, but how to remove those uncombined record from nPG_Chemical.NPG_Chemical_Symptoms? – Layla Wang Dec 11 '15 at 17:21

0 Answers0