Note that there are some explanatory texts on larger screens.

plurals
  1. POThere has to be a better way to add these using LINQ, right?
    text
    copied!<p>I am new to LINQ and and come up with the below to add new information to my DB using LINQ and EF5 but I am sure there is a more efficant, better, way to do this I just don't know it. I was hoping to get some input on what I can do to acceive the same but with less/more efficant code.</p> <pre><code> using (var db = new FullContext()) { if (ddlItemType.SelectedValue == "Other") { var NewItemType = new ItemType { Name = tbNewType.Text }; db.ItemTypes.Add(NewItemType); db.SaveChanges(); } if (ddlRegion.SelectedValue == "Other") { var NewRegion = new ReleaseRegion { Name = tbNewRegion.Text }; db.Regions.Add(NewRegion); db.SaveChanges(); } var NewItemTypeID = byte.Parse((from i in db.ItemTypes where i.Name == tbNewType.Text select new { i.ID }).ToString()); var NewRegionID = byte.Parse((from r in db.Regions where r.Name == tbNewRegion.Text select new { r.ID }).ToString()); var NewItem = new Item { Name = tbItemName.Text, TypeID = NewItemTypeID, RegionID = NewRegionID, Condition = ddlCondition.SelectedValue.ToString(), UPC = tbUPC.Text, ISBN = tbISBN.Text, IsColleciton = cbIsCollection.Checked, CollectionID = Convert.ToInt16(ddlCollection.SelectedValue), Notes = tbNotes.Text }; db.Items.Add(NewItem); db.SaveChanges(); } </code></pre> <p>Item.cs:</p> <pre><code>using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace FFCollection.DAL { [Table("Items")] public class Item { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Int16 ID { get; set; } [Required] public string Name { get; set; } public byte TypeID { get; set; } [ForeignKey("TypeID")] public virtual ItemType Type { get; set; } public byte RegionID { get; set; } [ForeignKey("RegionID")] public virtual ReleaseRegion Region { get; set; } [Required] public string Condition { get; set; } public string UPC { get; set; } public string ISBN { get; set; } public string Notes { get; set; } [Required] public Boolean IsColleciton { get; set; } public Int16 CollectionID { get; set; } [ForeignKey("CollectionID")] public virtual Item InCollectionID { get; set; } } } </code></pre> <p>ItemType.cs:</p> <pre><code>using System.ComponentModel.DataAnnotations.Schema; namespace FFCollection.DAL { public class ItemType { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public byte ID { get; set; } public string Name { get; set; } } } </code></pre> <p>The databinding to DDL:</p> <pre><code>using (var db = new FullContext()) { ddlItemType.DataSource = (from t in db.ItemTypes select new { t.ID, t.Name }).ToList(); ddlItemType.DataTextField = "Name"; ddlItemType.DataValueField = "ID"; ddlItemType.DataBind(); ddlItemType.Items.Insert(0, new ListItem("Other", "Other")); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload