I am trying to understand the syntax of a LINQ query. I tried creating one to select all rows from my TRACK_INFO table where the column collegeOf was equal to a variable. My database name is KuPlan. Below is the query I tried to create and my TRACK_INFO model. The error i get is: "could not find an implementation of the query pattern for source type KU_PLAN_DEV.Models.TRACK_INFO. 'Where' not found."
controller:
var query = from degreeName in TRACK_INFO
where degreeName == trackButton
select degreeName;
model:
namespace KU_PLAN_DEV.Models
{
using System;
using System.Collections.Generic;
public partial class TRACK_INFO
{
public TRACK_INFO()
{
this.CORE_HEAD = new HashSet<CORE_HEAD>();
this.GEN_ED_HEAD = new HashSet<GEN_ED_HEAD>();
this.GEN_ED_NOTE = new HashSet<GEN_ED_NOTE>();
this.GRAD_CLEAR_HEAD = new HashSet<GRAD_CLEAR_HEAD>();
this.MAJOR_NOTE = new HashSet<MAJOR_NOTE>();
}
public string progNum { get; set; }
public string versionNum { get; set; }
public string degreeName { get; set; }
public string collegeOf { get; set; }
public string effectiveDateTerm { get; set; }
public Nullable<decimal> effectiveDateYear { get; set; }
public string trackDegreeType { get; set; }
public virtual ICollection<CORE_HEAD> CORE_HEAD { get; set; }
public virtual ICollection<GEN_ED_HEAD> GEN_ED_HEAD { get; set; }
public virtual ICollection<GEN_ED_NOTE> GEN_ED_NOTE { get; set; }
public virtual ICollection<GRAD_CLEAR_HEAD> GRAD_CLEAR_HEAD { get; set; }
public virtual GRAD_CLEAR_SIG_DATE GRAD_CLEAR_SIG_DATE { get; set; }
public virtual ICollection<MAJOR_NOTE> MAJOR_NOTE { get; set; }
}
}
TRACK_INFO is a class name, not an IEnumerable<TRACK_INFO>. I think you meant to get a property off of your context:
from degreeName in context.TRACK_INFO
...
Related
I have a DomainModel and a DTO like this :
public class PostEntity: IEntity
{
[Required]
public string Description { get; set; }
public int Id { get; set; }
[Required]
public string Slug { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public string Title { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public PostStatus Status { get; set; }
public User Writer { get; set; }
public int WriterId { get; set; }
}
public class PostDto
{
public int Id { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
public string Tags { get; set; }
public string Title { get; set; }
public DateTime CreatedOn { get; }
public List<string> TagList { get; set; }
public PostDto()
{
TagList = new List<string>();
}
}
PostEntity'Tags contains some tags seperated by ",", now I want to split tags value by "," and convert it to List, to do this, I've tried this but I get the below compilation error
CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(",").ToList()));
I get this error :
An expression tree may not contain a call or invocation that uses optional arguments
I can't reproduce your error, it seems to work fine.
Below is an example where the TagList is correctly mapped
The code I used :
MapperConfiguration MapperConfiguration = new MapperConfiguration(configuration =>
{
configuration
.CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(',').ToList()));
});
IMapper mapper = MapperConfiguration.CreateMapper();
PostEntity postEntity = new PostEntity
{
Tags = "Tag1,Tag2,Tag3,Tag4"
};
var mappedObject = mapper.Map<PostEntity, PostDto>(postEntity);
Please bear in mind that Expression.Call API does not support optional parameters. So, you should Replace Split(',') with
Split(',', System.StringSplitOptions.None)
or
Split(',', System.StringSplitOptions.RemoveEmptyEntries)
doing so you won't see that error again.
I have this kind of structure stored in Raven DB
public class AAA
{
public int Id { get; set; }
public string Name { get; set; }
}
public class BBB
{
public int Id { get; set; }
public string Name { get; set; }
public List<int> AAAIds { get; set; }
}
and I'm trying to get a query to basically show something like:
public class AAA_BBB
{
public int AAB_Id { get; set; }
public string AAA_Name { get; set; }
public bool ContainsAtLeastOneBBB { get; set; }
}
I'm trying to do something like:
var AAA_BBB = session.Query().Include(a=>a.AAAIds.Select(a=>a))
You need something like:
session.Query<BBB>().Include<BBB,AAA>(x=>x.AAAIds)
I think entity framework has problem while generating database in my project. That's strange that it only happens in one case. This is one to many relationship between "User" and "Playlist". One User has many Playlists.
Here is my code, I used some abstract classes in my project.
Core code
Playlist class
public virtual User User { get; set; }
User class
public virtual ICollection<Playlist> Playlists { get; set; }
The full code:
Generic class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace xxx.Areas.admin.Models
{
public abstract class Generic
{
[Display(Name = "Ngày tạo")]
public DateTime? Created { get; set; }
[Display(Name = "Lần sửa cuối")]
public DateTime? Modified { get; set; }
[Display(Name = "Trạng thái")]
public bool? IsActive { get; set; }
}
}
Post class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace xxx.Areas.admin.Models
{
public abstract class Post : Generic
{
public string Title { get; set; }
public string Slug { get; set; }
public string Content { get; set; }
public string Image { get; set; }
public int Views { get; set; }
public bool? AllowComment { get; set; }
public User ModifiedBy { get; set; }
public virtual ICollection<Media> Medias { get; set; }
}
}
AlbumBase class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using xxx.Areas.admin.Models.SongBase;
namespace xxx.Areas.admin.Models.AlbumBase
{
public abstract class AlbumBase : Post
{
public bool IsPublic { get; set; }
public bool IsFeatured { get; set; }
public int OldID { get; set; }
public string OldSlug { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
}
Playlist class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using xxx.Areas.admin.Models.SongBase;
namespace xxx.Areas.admin.Models.AlbumBase
{
public class Playlist : AlbumBase
{
[Key]
public int PlaylistID { get; set; }
public virtual ICollection<Song> Songs { get; set; }
public virtual ICollection<Folk> Folks { get; set; }
public virtual ICollection<Instrumental> Instrumentals { get; set; }
public virtual User User { get; set; }
public Playlist()
{ }
public Playlist(string name)
{
Title = name;
Slug = Functions.URLFriendly(Title);
Views = 0;
OldID = 0;
AllowComment = true;
IsActive = true;
IsPublic = false;
IsFeatured = false;
Created = DateTime.Now;
}
}
}
and User class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using baicadicungnamthang.Areas.admin.Models.AlbumBase;
using baicadicungnamthang.Areas.admin.Models.Social;
using baicadicungnamthang.DAL;
using ICB;
namespace xxx.Areas.admin.Models
{
public class User : Generic
{
[Key]
public int UserID { get; set; }
[Required(ErrorMessage = "Bạn phải nhập tên tài khoản"), StringLength(50)]
public string UserName { get; set; }
public string Password { get; set; }
public string HashPassword { get; set; }
[Required(ErrorMessage = "Bạn phải nhập địa chỉ email"), EmailAddress(ErrorMessage = "Địa chỉ email không hợp lệ")]
public string Email { get; set; }
[StringLength(50)]
public string NickName { get; set; }
public string FullName { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string Phone { get; set; }
public string Avatar { get; set; }
public DateTime? DOB { get; set; }
[StringLength(1)]
public string Gender { get; set; }
public string Address { get; set; }
public int TotalLikes { get; set; }
public int TotalComments { get; set; }
public int Views { get; set; }
public string ActivationKey { get; set; }
public string RecoverKey { get; set; }
public DateTime? LastLogin { get; set; }
public int OldID { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Comment> RateComments { get; set; }
public virtual ICollection<Playlist> Playlists { get; set; }
public virtual ICollection<User> Friends { get; set; }
public virtual ICollection<Message> MessagesSent { get; set; }
public virtual ICollection<Message> MessagesReceived { get; set; }
public User()
{
Created = DateTime.Now;
IsActive = false;
TotalLikes = 0;
Views = 0;
OldID = 0;
}
public string getAvatar(int w, int h)
{
return Functions.getAvatarThumb(UserName, w, h);
}
public int getAge()
{
if (DOB == null)
{
return 0;
}
else
{
DateTime now = DateTime.Now;
int age = now.Year - DOB.Value.Year;
return age;
}
}
public string getGender()
{
if (Gender == "M")
{
return "Nam";
}
else if (Gender == "F")
{
return "Nữ";
}
else return "";
}
}
}
And this is Playlist table generating from code first:
As you see, entity framework has generated two columns: User_UserID and User_UserID1 from primary key UserID of User table.
I say that because when I uncomment the line
//public virtual User User { get; set; }
and rebuild the project, the two column User_UserID and User_UserID1 has disappeared too.
The problem only happens with User and Playlist relationship. With other one-to-many scenarios (User-Comments), system work well.
Can anyone give me a suggestion?
The problem is that you have multiple relationships between the same entities.
Playlist has 2 references to User (one in the Playlist class and one in its base class Post).
This confuses Entity Framework, because it doesn't know how to map the relationships, which is why it creates too many foreign keys in the database.
To fix this you can use the InverseProperty attribute to tell it how to map the navigation properties:
public class Playlist : AlbumBase
{
[Key]
public int PlaylistID { get; set; }
[InverseProperty("Playlists")]
public virtual User User { get; set; }
......
I have the following entity framework code snippet which has a "Groups" table and a child "ApplicationsGroupsLK" table that contains an ApplicationID field that I need.
IEnumerable<Groups> Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p => p.GroupNumber > 0);
The child data comes back obviously in a collection.
I basically need to display the parent data along with the child ApplicationID field (many Applications to 1 Group).
In my MVC View, what should my ViewModel look like that would contain the parent and child data coming back that I need in order that I can properly bind it to a grid?
Second post:
Further, the following model was generated from Entity Framework:
public partial class Project
{
public Project()
{
this.TimeTrackings = new HashSet<TimeTracking>();
}
[DataMember]
public short ProjectID { get; set; }
[DataMember]
public short CustomerID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public short CategoryID { get; set; }
[DataMember]
public short PriorityID { get; set; }
[DataMember]
public short StatusID { get; set; }
[DataMember]
public Nullable<decimal> Quote { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public virtual Category Category { get; set; }
[DataMember]
public virtual Customer Customer { get; set; }
[DataMember]
public virtual Priority Priority { get; set; }
[DataMember]
public virtual Status Status { get; set; }
[DataMember]
public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
}
You can see that TimeTrackings is the child table of Project. You can also see that CategoryID, CustomerID, PriorityID, and StatusID are foreign keys that the parent table has. In this case, I'm only interested in the CategoryID FK.
I haven't done this yet (not at my machine at home), but when I get the data into this model, what would actually be contained in the public virtual Category Category field? Since it's not a collection, what data is returned in this field after the query executes.
Third post:
Telerik asp.net for mvc syntax for DB call:
IEnumerable<Groups> GroupList = db.GetGroups();
return View(new GridModel<Groups>
{
Data = GroupList
});
Fourth post:
Trey, below is the code I modified for my purposes and was hoping you can check it over before I implement it. I think I undersand it and seems great...
public partial class Project
{
public Project()
{
this.TimeTrackings = new HashSet<TimeTracking>();
}
[DataMember]
public short ProjectID { get; set; }
[DataMember]
public short CustomerID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public short CategoryID { get; set; }
[DataMember]
public short PriorityID { get; set; }
[DataMember]
public short StatusID { get; set; }
[DataMember]
public Nullable<decimal> Quote { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public short ApplicationID { get; set; }
[DataMember]
public string ApplicationName { get; set; }
[DataMember]
public virtual Category Category { get; set; }
[DataMember]
public virtual Customer Customer { get; set; }
[DataMember]
public virtual Priority Priority { get; set; }
[DataMember]
public virtual Status Status { get; set; }
[DataMember]
public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
public ProjectModel(Project project)
{
ProjectID = project.ProjectID;
CustomerID = project.CustomerID;
Name = project.Name;
Description = project.Description;
CategoryID = project.CategoryID;
PriorityID = project.PriorityID;
StatusID = project.StatusID;
Quote = project.Quote;
Notes = project.Notes;
CreatedDate = project.CreatedDate;
UpdatedDate = project.UpdatedDate;
ApplicationID = project.ApplicationsGroupsLK.ApplicationID;
ApplicationName = project.ApplicationsGroupsLK.ApplicationName;
}
// Neat Linq trick to convert database query results directly to Model
public static IList<ProjectModel> FlattenToThis(IList<Project> projects)
{
return projects.Select(project => new ProjectModel(project)).ToList();
}
}
Fifth post:
using (wmswebEntities DbContext = new wmswebEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Projects> projects = DbContext.Projects.Where(p => p.GroupNumber > 0);
IList<ProjectModel> results = Project.FlattenToThis(projects);
return results
}
Sixth post
namespace CMSEFModel
{
using System;
using System.Collections.Generic;
public partial class GroupModel
{
public GroupModel()
{
this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
}
public int GroupNumber { get; set; }
public string GroupName { get; set; }
public int GroupRank { get; set; }
public bool ActiveFlag { get; set; }
public System.DateTime DateAdded { get; set; }
public string AddedBy { get; set; }
public System.DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }
// Application - Lazy Loading population
public int ApplicationID { get; set; }
// UsersGroupsLK - Lazy Loading population
public int UserNumber { get; set; }
public string UserID { get; set; }
public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }
public GroupModel()
{}
public GroupModel(GroupModel group)
{
GroupNumber = group.GroupNumber;
GroupName = group.GroupName;
ActiveFlag = group.ActiveFlag;
DateAdded = group.DateAdded;
AddedBy = group.AddedBy;
LastUpdated = group.LastUpdated;
LastUpdatedBy = group.LastUpdatedBy;
UserNumber = group.UsersGroupsLKs.
}
// Neat Linq trick to convert database query results directly to Model
public static IList<GroupModel> FlattenToThis(IList<GroupModel> groups)
{
return groups.Select(group => new GroupModel(group)).ToList();
}
}
}
Seventh Post - this is the model I am having trouble with about the errors I previously posted. Trey, if you could help, I WOULD REALLY APPRECIATE IT.... I'm "dead in the water" unless I can get this part working.
namespace CMSEFModel
{
using System;
using System.Collections.Generic;
public partial class Group
{
public Group()
{
this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
}
public int GroupNumber { get; set; }
public string GroupName { get; set; }
public int GroupRank { get; set; }
public bool ActiveFlag { get; set; }
public System.DateTime DateAdded { get; set; }
public string AddedBy { get; set; }
public System.DateTime LastUpdated { get; set; }
public string LastUpdatedBy { get; set; }
// Application - Lazy Loading population
public int ApplicationID { get; set; }
// UsersGroupsLK - Lazy Loading population
public int UserNumber { get; set; }
public string UserID { get; set; }
public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }
public GroupModel(Group group)
{
GroupNumber = group.GroupNumber;
GroupName = group.GroupName;
ActiveFlag = group.ActiveFlag;
DateAdded = group.DateAdded;
AddedBy = group.AddedBy;
LastUpdated = group.LastUpdated;
LastUpdatedBy = group.LastUpdatedBy;
UserNumber = group.UsersGroupsLKs.
}
// Neat Linq trick to convert database query results directly to Model
public static IList<GroupModel> FlattenToThis(IList<Group> groups)
{
return groups.Select(group => new GroupModel(group)).ToList();
}
}
}
Eight post:
using (wmswebEntities DbContext = new wmswebEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = true;
DbContext.Database.Connection.Open();
List<Groups> myGroups = new List<Groups>();
var myGroups = from p in DbContext.Groups
where p.ActiveFlag = true
select new
{
p.Groups.ApplicationName,
p.Groups.GroupName,
p.Groups.GroupRank,
p.Groups.ActiveFlag,
p.Groups.DateAdded,
p.Groups.AddedBy,
p.Groups.LastUpdated,
p.Groups.LastUpdatedBy,
p.Groups.ApplicationsGroupsLK.ApplicationID,
p.Groups.UsersGroupsLK.UserNumber
};
return myGroups;
}
This will take a slight tweak in thinking. The Grid will only accept a flat model. This type of question is asked often, here is a starter answer: Kendo UI Grid - How to Bind to Child Properties
If that does not help, post more of your code here and I can work through it with you.
RavenDB docs show how to deal with document relationships in this sample using Includes.
public class Order
{
public Product[] Items { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public string[] Images { get; set; }
public double Price { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public short Age { get; set; }
public string HashedPassword { get; set; }
}
How would I deal with Includes or Live Projections if I don't want to include the customer using Includes/Live Projections but a list of products instead:
public class Order
{
public string[] ItemIds { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}
If I understand what you're asking, this should help. I blogged about it here:
http://inaspiralarray.blogspot.com/2012/03/keeping-domain-model-pure-with-ravendb.html
Does that help?