Linq update with an Ilist - c#

I would like to update my database datas and I have the following 2 "lists" of element :
Linq Query returning me an IQueryable<VM_CategoryLabel> from the database (that has to be updated)
IList<VM_CategoryLabelExtra> (list coming from a web interface)
Here are my 2 classes:
public class VM_CategoryLabel
{
public int Id { get; set; }
public int Order { get; set; }
public string Label { get; set; }
public string Unit { get; set; }
public bool Checked { get; set; }
}
public class VM_CategoryLabelExtra
{
public int Id { get; set; }
public int IdCat { get; set; }
public int Order { get; set; }
public string Label { get; set; }
public string Unit { get; set; }
public bool Checked { get; set; }
}
It seems so basic, but I get an error and was unsuccessful trying with Linq to join or with the let. Maybe the way I am doing it is totally wrong ?

Related

Nested Collection Mapping in Automapper

Am trying to map nested collections using automapper and I have done the basic setup and configuration. When I try to do the map it the nested values are coming as null. I have tried to follow few posts and put together something. I want the list to have a hierarchy instead of flattening. Any help around this would be great.
Source Entities:
public class OuterEntity
{
public int ID { get; set; }
public string Name { get; set; }
public List<InnerEntity> InnerEntityList { get; set; }
}
public class InnerEntity
{
public int InnerId { get; set; }
public string InnerName { get; set; }
public List<InnerMostEntity> InnerMostList { get; set; }
}
public class InnerMostEntity
{
public int InnerMostId { get; set; }
public string InnerMostName { get; set; }
public DateTime ModifiedDate { get; set; }
}
Destination Entities:
public class OuterEntityDTO
{
public int ID { get; set; }
public string Name { get; set; }
public List<InnerEntity> InnerEntityList { get; set; }
}
public class InnerEntityDTO
{
public int InnerId { get; set; }
public string InnerName { get; set; }
public List<InnerMostEntity> InnerMostList { get; set; }
}
public class InnerMostEntityDTO
{
public int InnerMostId { get; set; }
public string InnerMostName { get; set; }
public DateTime ModifiedDate { get; set; }
}
Controller Class:
public List<OuterEntityDTO> GetAll()
{
var outerEntityList = myRepo.GetAll(); //Type of List<OuterEntity>
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<OuterEntity, OuterEntityDTO>().ReverseMap();
cfg.CreateMap<InnerEntity, InnerEntityDTO>().ReverseMap();
cfg.CreateMap<InnerMostEntity, InnerMostEntityDTO>().ReveseMap();
});
config.AssertConfigurationIsValid();
var innerMostDTO = Mapper.Map<List<OuterEntity>,List<OuterEntityDTO>>(outerEntityList);
//The inner list at first level itself is null.
return innerMostDTO;
}
Am trying to achieve this in DOT NET Core. Autommaper version is 6.1.1
I think you should have a wrong class hierarchy in DTO classes, as you have
public List<InnerMostEntity> InnerMostList { get; set; }
in public class InnerEntityDTO, you should write it as
public List<InnerMostEntityDTO> InnerMostList { get; set; }

Custom Relationship in Entity Framework

I have got the following three tables (just an example)
public class User
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Device
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Code
{
public int id { get; set; }
public int entity_id { get; set; }
public string entity_type { get; set; }
public string code { get; set; }
}
Now a code can either belong to a User or a Device, which will be determined by the value of entity_type (i.e. 'user' or 'device'). How can this be achieved in Entity Framework ?
You could change your Code class to
public class Code
{
public int id { get; set; }
public User user { get; set; }
public Device device { get; set; }
public string code { get; set; }
}
This way one of those properties can be null.
Hope it helps.
The drawback is that you could have a code that have a user an a device

PHP error with multiple levels of key-value JSON pairs

I am using multiple levels of JSON data coming into php from a C# application, as in:
public class User_Group
{
public int ID_UserGroup { get; set; }
public string Name_UserGroup { get; set; }
public int UserID { get; set; }
}
public class User_Role
{
public int ID_User { get; set; }
public string Role_User { get; set; }
public string User_Role_Description { get; set; }
public List<User_Group> UserGroup { get; set; }
}
public class Stand_Orte
{
public int ID { get; set; }
public string Bezeichnung { get; set; }
public List<Modul> modul { get; set; }
}
public class Modul
{
public string ID { get; set; }
public string Seriennummer { get; set; }
public string Bezeichnung { get; set; }
public string StandortID { get; set; }
public List<Mess_Kanal> MessKanal { get; set; }
}
public class Mess_Kanal
{
public string ID { get; set; }
public string ModulID { get; set; }
public List<LogMess_Daten> LogMessDaten { get; set; }
}
public class LogMess_Daten
{
public string KanalID { get; set; }
public string Zeitstempel { get; set; }
}
public class RootObject
{
public int ID_Project { get; set; }
public string Name_Project { get; set; }
public int Receiver_ID { get; set; }
public string Receiver_Name { get; set; }
public int UserID { get; set; }
public User_Role UserRole { get; set; }
public Stand_Orte Standorte { get; set; }
}
I have an issue with accessing the 3rd level data elements.
For Eg., I am able to get the values uptil Project-> Stand_Orte-> Modul. but after that Project-> Stand_Orte-> Modul-> MessKanal Throws an error in PHP as " Trying to get property of non-object ".
I tried the following:
$phpArray['project']->Standorte[0]->modul[0]->MessKanal[0]->ID
$messkanals=$phpArray->Standorte->modul->MessKanal;
And then used a "foreach ($messkanals as $messkanal)" to insert MessKanal data into MYSQL.
it gives me the following errors respectively.
Cannot use object of type stdClass as array
Trying to get property of non-object
does anyone have any idea?
Thanks,
Revathy

Code First Empty Navigation Property

I have a really weird issue going on that I can't seem to find the answer on google for.
I currently have the following Code First/DB structure:
public class Prospect {
[Key]
public int ProspectId { get; set; }
public int ProspectCompanyId { get; set; }
public int? ImportUserId { get; set; }
public int? ProspectUserId { get; set; }
public int? SalesUserId { get; set; }
public int? CampaignId { get; set; }
[ForeignKey("CampaignId")]
public virtual ICollection<CampaignEvent> CampaignEvents { get; set; }
[ForeignKey("CampaignId")]
public virtual Campaign Campaign { get; set; }
}
public class CampaignEvent
{
[Key]
public int CampaignEventId { get; set; }
public int CampaignId { get; set; }
public int UserId { get; set; }
public string ActionType { get; set; }
public string ActionSubject { get; set; }
public string ActionTemplate { get; set; }
}
The problem is that when I try to return a query like:
var prospects = db.Prospects.Include("CampaignEvents");
The prospects.First().CampaignEvents is empty - when it should be populated with records.
I've even taken the SQL generated by the Entity Framework and executed it with it successfully returning the prospect and campagin events directly in the database.
I'm at a loss of what to do - its very odd, I receive no errors, just an empty collection.
Please let me know if you need any additional details!
try with
var prospects = db.Prospects.Include("CampaignEvents");
var value = prospects.FirstOrDefault(p=>p.CampaignEvents.Any());

Automatic Creating of Views with dropdown on 1:n relations

I followed a Tutorial a few weeks ago which shows how to create a simple ASP.NET MVC 3 App (http://www.asp.net/mvc/tutorials/mvc-music-store-part-1).
Now I created another app that is actually doing something very simple, but I cant make Visual Studio 2010 automatically creating a View that shows a selection of the 1:n connection.
The program is a simple News system with a relation NewsEntry.NewsCategory to NewsCategory.ID.
NewsEntry.cs
public class NewsEntry
{
public int ID { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public NewsCategory NewsCategory { get; set; }
}
NewsDB.cs
public class NewsCategory
{
public int ID { get; set; }
public string Name { get; set; }
public List<NewsEntry> News { get; set; }
}
NewsDB.cs
public class NewsDB : DbContext
{
public DbSet<NewsEntry> NewsEntry { get; set; }
public DbSet<NewsCategory> NewsCategory { get; set; }
}
So my question is what is missing that VS is not creating a view with category in a drop down list?
It obviously helped to add a NewsCategoryId field:
public class NewsEntry
{
public int NewsEntryId { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public int NewsCategoryId { get; set; }
public virtual NewsCategory NewsCategory { get; set; }
}

Categories

Resources