I have 2 entities: Post and Comment
I have a Post instance and I want to get its first 10 comments.
Post:
public class Post
{
public int Id { get; set; }
[InverseProperty("Post")]
public virtual ICollection<Comment> Comments { get; set; }
}
Comment:
public class Comment
{
public int Id { get; set; }
[Required]
public string Text { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
}
How many Comments will come from the database if I do this? All or just 10?
var comments = post.Comments.Take(10).ToList();
Is it more efficient to do this?
var comments = Db.Comments.Where(x => x.Post == post).Take(10).ToList();
If you use .Include() then the property with InverseProperty attribute will contain all Comments assigned to this Post.
So it's better not to use it if you want to get only the first 10 comments.
Related
I want first to say sorry, because I'm new to this programming language, so forgive me if I say or do something wrong.
I created a project with 3 class libraries (1 of them contains the tables from sql server). I made a ViewModel based on the tutorial from: "http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example", and now I want to be able to update the data in those tables, but I don't know how. I tried to do something but it failed.
`namespace BOL2.ViewModel
{
public class NIRIO
{
[Key]
public int ID { get; set; }
public int Number { get; set; }
public Nullable Date { get; set; }
public int NirID { get; set; }
[DisplayName("TypeID")]
public int TipID { get; set; }
public int SupplierID { get; set; }
public int ProductID { get; set; }
public Nullable<System.DateTime> EntryDate { get; set; }
public Nullable<System.DateTime> ExitDate { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
public BOL2.tbl_NIR tbnir;
public BOL2.tbl_I_O tblio { get; set; }`
This is my ViewModel. It contains data from those 2 tables (tbl_NIR, first 3, and the others from tbl_I_O. I saw something on my research that they had a repository class, but I don't now if I should do another class for the viewmodel or I sould use the 2 that I already have? Any help is greatly appreciated.
You could do something similar to this.
public void UpdateCar(CarViewModel viewModel)
{
using (DataContext context = new DataContext())
{
CarEntity dataModel = context.CarEntities.where(x => x.Id == viewModel.Id).First();
dataModel.Name = viewModel.Name;
dataModel.Type = viewModel.Type;
context.SaveChanges();
}
}
You need to create your model objects from your view model and set the values for the models.
I'm not quite sure what is the method you used and didn't work. Just in case you tried to update the tables through the view, then you cannot do that. Because you have join
inform IT explanation
I recommend you to create a stored procedure in your database. Then call the procedure trough the code. It's secure and fast. explained here
I have an MVS project where I use an AJAX to retrieve an array...
The following is my class on C#... I retrieve a list of 19,500 records... basically I pull an IEnumerable
public partial class VW_Suppliers
{
public int SupplierID { get; private set; }
public string Name { get; private set; }
public int SolarNumber { get; private set; }
public bool TpiClassification { get; private set; }
public int ValueStreamID { get; private set; }
public string ValueStream { get; private set; }
public int StatusID { get; private set; }
public string Status { get; private set; }
public string SupplierCode { get; private set; }
public int PurchaseSystemID { get; private set; }
public string PurchaseSystem { get; private set; }
public int AddressID { get; private set; }
public int AddressTypeID { get; private set; }
public string AddressType { get; private set; }
public string Street1 { get; private set; }
public string Street2 { get; private set; }
public string PoBox { get; private set; }
public string City { get; private set; }
public string PostalCode { get; private set; }
public int StateID { get; private set; }
public string StateCode { get; private set; }
public string State { get; private set; }
public int CountryID { get; private set; }
public string CountryCode { get; private set; }
public string Country { get; private set; }
}
[HttpGet]
public ActionResult GetSuppliersData()
{
JsonObject jsonObject;
IEnumerable<ViewSupplierVM> suppliers = _supplierService.GetAllSuppliers();
JsonResult jsonResult = Json(suppliers, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
However, I noticed two things... when using breakpoints I noticed that after I hit return... it takes around 10 seconds for AngularJS to hit the breakpoint when the data is received. Second problem is that whenever the page is ready with all data... the overall site is rather slow... for instance.. you type on a textbox, and everything appears from 0.5 to 4 seconds after typing... which leads me to believe that that array of 19,500 elements is consuming to much memory...
It is required that all suppliers appear on the first screen, so there is no way to turn this requirement around.
Any tips? What tricks could be done on the MVC controller to send to front end faster and prevent site from being to slow?
Loading and rendering 19,000 is in no way viable to me. I too just worked on an application in which a customer would need to be able to manage all their products (15,000+). The initial implementation was done using pagination for which this answer solved it out really nicely for me. But a more elegant solution is to implement some type of infinite-scroll mechanism, for which I have used ngInfiniteScroll (https://sroze.github.io/ngInfiniteScroll/). Now this solves one part of the problem for you, and that is your page being slow due to massive HTML renders, but in no case retrieving 19,000 items from the server in one shot is a good idea. Not only it stresses the database immensely, but if hosted on a live server you shall see quite a significant delay when the request for the data is made. I suggest pulling your data 20, 30, 100 at a time. For this myPagingFunction from ngInfiniteScroll should be just the right place to implement.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Newbie here.
I've got a C# Windows Presentation Forms program and I'm trying to add a method that downloads the string of a JSON webpage and then creates a list of events (each of which has a list of teams attending) from the data in the page. The only problem is that the JSON.NET documentation is confusing me and I can't seem to get a grasp on JSON format in general.
For reference, there are two parts to this method:
1) get the list of "official" events (if you look you will notice there is an "official" boolean value in each event item) and create a string list containing each event "key" (the first value in each event item)
2) for each event "key", make a list of teams attending each event and sort each team by their "team_number"
Here is a test page for the event list. Here is the test page for the team list from one event.
My biggest problem with the JSON.NET library/documentation is that I can't seem to figure out how to take each item and isolate one specific value from it. For example, there are a whole bunch of values in each event item, but I only need the "official" value and the "key" value. Same with the teams, I only need the "team_number" value from each item.
Can you guys help me understand the library a little bit more or maybe even point me to the correct library if this is the wrong one for the job?
First thing to do is use json2csharp to generate the classes that represent your JSON.
public class Event
{
public string key { get; set; }
public string website { get; set; }
public bool official { get; set; }
public string end_date { get; set; }
public string name { get; set; }
public string short_name { get; set; }
public string facebook_eid { get; set; }
public string event_district_string { get; set; }
public string venue_address { get; set; }
public int? event_district { get; set; }
public string location { get; set; }
public string event_code { get; set; }
public int year { get; set; }
public List<object> webcast { get; set; }
public List<object> alliances { get; set; }
public string event_type_string { get; set; }
public string start_date { get; set; }
public int event_type { get; set; }
}
public class Team
{
public string website { get; set; }
public string name { get; set; }
public string locality { get; set; }
public int rookie_year { get; set; }
public string region { get; set; }
public int team_number { get; set; }
public string location { get; set; }
public string key { get; set; }
public string country_name { get; set; }
public string nickname { get; set; }
}
Now you can deserialize the JSON into lists of those objects and use LINQ to query what you need out of the list. I'm doing this from memory so it may need some tweaking to work.
var events = JsonConvert.Deserialize<IEnumerable<Event>>( eventJson );
var officialEvents = events.Where( e => e.official )
.Select( e => new { e.key, e.official } );
i am hanging for while and i dont understand one thing...I will try to explain my issue...
I have model(Table) called FeuerwehrPerson and the class looks like that:
public partial class FeuerwehrPerson
{
public FeuerwehrPerson()
{
this.BuchungenResultateList = new List<BuchungenResultate>();
this.Notenspiegel=new Notenspiegel();
this.Gemeinde=new Gemeinde();
}
public int FeuerwehrPersonId { get; set; }
public string Anrede { get; set; }
public string Vorname { get; set; }
public string Nachname { get; set; }
public System.DateTime Geburtstag { get; set; }
public string Strasse { get; set; }
public string Plz { get; set; }
public string Ort { get; set; }
public string Telefon { get; set; }
public int GemeindeId { get; set; }
public Nullable<int> NotenspielgelId { get; set; }
public virtual ICollection<BuchungenResultate> BuchungenResultateList { get; set; }
public virtual Gemeinde Gemeinde { get; set; }
public virtual Notenspiegel Notenspiegel { get; set; }
}
In Feuerwehrperson are 2 Foreign Keys
GemeindeId
NotenspiegelId
and both of them are holding some values for a specific FeuerwehrPerson.
Both Foreign Keys are not null in my FeuerehrPerson Table...
In my Controller i have a Edit Action...and i am searching for FeuerwehrPerson by id...thats working fine:
FeuerwehrPerson feuerwehrperson = db.FeuerwehrPerson.Find(id);
When i am checking the values by using add Watch of feuerwehrperson Object there are some empty properties which shouldn´t be empty
The Object property Gemeinde should contain some values but its empty...also for Notenspiegel Object should contain SeminarA=true, SeminarB=true, SeminarC=true, rest is false but everything is false also NotenspiegelId and GemeindeId...but the Foreign Key references in Feuerwehrperson are not null...but why is it empty????
Please help me...
I'm just learning ASP.NET MVC 3, And recently I tried a lot of times to pass arrays/lists/ICollections etc. but couldn't. everytime the list was empty.
For example, the current project:
Model:
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<string> Tags { get; set; }
}
Initializer - Seed:
protected override void Seed(DatabaseContext context)
{
var videos = new List<Video>
{
new Video {
Name = "Video01",
Tags = new List<string> { "tag1", "tag2" },
};
videos.ForEach(s => context.Videos.Add(s));
context.SaveChanges();
base.Seed(context);
}
In the view: I do get the Name property, but the Tags are completely empty.
In the debug I get Tags - Count: 0.
This is not the first time it happens to me, to be honest it happens every single time when I try to pass those kind of stuff. a bit of info about the project:
ASP.NET MVC 3, Entity-Framework:Code First, SqlServerCe.4.0.
Crean an entity Tag
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public int VideoId { get; set; }
public string TagText { get; set; }
}
or store tags to one field separated with comma /semicolon or whatever fits for your solution
By default Entity Framework doesn't load associations of an entity, you need to specify it explicitly:
var videos = context.Videos.Include("Tags");