Create Dynamic Expression one to many relations with child class property by dynamic-linq - dynamic-linq-core

Is it possible to create a dynamic where condition with a collection property?
I'm trying to filter Parents which Childs.ChildProp1 == "test".
What's the correct syntax ?
[Framework Url] https://github.com/zzzprojects/System.Linq.Dynamic.Core
public class Parent
{
public string MasterProp1 { get; set; }
public string MasterProp2 { get; set; }
public IEnumerable<Child> Childs { get; set; }
}
public class Child
{
public string ChildProp1 { get; set; }
public int ChildProp2 { get; set; }
}
Parents.Where("Childs.ChildProp1==\"test\"")

Related

how can I Select One row where max StartDate in list objects using linq

how can I Select One row json where max StartDate in list objects
using linq tolist
{"itmes":[
{"StartDate":"20190901185703","Name":"A1","Id":"1","EndDate":"20190930235959"}
,{"StartDate":"20190903181510","Name":"A2","Id":"2","EndDate":"20190909235959"}
,{"StartDate":"20190906005152","Name":"A3","Id":"3","EndDate":"20191006235959"}
,{"StartDate":"20190714181313","Name":"A4","Id":"4","EndDate":"20991231235959"}
],"Code":"0","text":"success"}
public class Query
{
public class itmes
{
public string StartDate { get; set; }
public string Name { get; set; }
public string Id { get; set; }
public string EndDate { get; set; }
}
public class RootObject
{
public List<itmes> itmes { get; set; }
public string Code { get; set; }
public string text { get; set; }
}
}
var result = JsonConvert.DeserializeObject<List<Query.RootObject>>(json);
Expected output This row tolist
{"StartDate":"20190906005152","Name":"A3","Id":"3","EndDate":"20191006235959"}
Firstly you cannot cast and object to collection so it should be Query.RootObject instead of this List<Query.RootObject> ,Secondly im not sure why did you make class inside a class you can do it independently without the query class
You classes would be
public class itmes
{
public string StartDate { get; set; }
public string Name { get; set; }
public string Id { get; set; }
public string EndDate { get; set; }
}
public class RootObject
{
public List<itmes> itmes { get; set; }
public string Code { get; set; }
public string text { get; set; }
}
var json = "{\"itmes\":[ {\"StartDate\":\"20190901185703\",\"Name\":\"A1\",\"Id\":\"1\",\"EndDate\":\"20190930235959\"} ,{\"StartDate\":\"20190903181510\",\"Name\":\"A2\",\"Id\":\"2\",\"EndDate\":\"20190909235959\"} ,{\"StartDate\":\"20190906005152\",\"Name\":\"A3\",\"Id\":\"3\",\"EndDate\":\"20191006235959\"} ,{\"StartDate\":\"20190714181313\",\"Name\":\"A4\",\"Id\":\"4\",\"EndDate\":\"20991231235959\"} ],\"Code\":\"0\",\"text\":\"success\"}";
var result = JsonConvert.DeserializeObject<RootObject>(json);
var row = result.itmes.Where(p=> p.StartDate == result.itmes.Max(q => q.StartDate)).ToList();

Trying to Populate ViewModel List from Linq query MVC "List doesn't contain definiton for ..."error"

I am (trying to) build a multiple choice test application that will pass a list of multiple choice questions (select lists) from the controller to a view.
Then populate the view using a foreach loop, post the answers back to the controller, check them and increment the score for each correct answer and then update the db.
I am trying to populate the view model list using a Linq query (to keep my controller thin, I am doing this via a method in my Service layer).
Models
Questions (db first)
namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;
public partial class Question
{
public Question()
{
this.ExamDets = new HashSet<ExamDet>();
}
public int QuID { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
public string Level_ { get; set; }
public string GrammarPoint { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
Exam
namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;
public partial class Exam
{
public Exam()
{
this.Candidates = new HashSet<Candidate>();
this.ExamDets = new HashSet<ExamDet>();
}
public int ExamID { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> Date { get; set; }
public int AdminID { get; set; }
public string StartLevel { get; set; }
public virtual Administrator Administrator { get; set; }
public virtual ICollection<Candidate> Candidates { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
There is also a model for Candidate that contains the CandID:
And the view model that uses these models:
namespace AccessEsol.Models
{
public class ExamQuestionsViewModel
{
public int QuID { get; set; }
public int CandID { get; set; }
public int ExamId { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
public string Level { get; set; }
public string GrammarPoint { get; set; }
public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}
This is the method that is to populate the view model:
public static List<ExamQuestionsViewModel> AddQuestions()
{
AccessEsolDataEntities db = new AccessEsolDataEntities();
string questionLevel = GetLevel();
int currentCand = GetCandID();
int currentExam = GetExamID();
//model = new DataAccess().Populate();
var qu = (from a in db.Questions
where a.Level_ == questionLevel
select a).ToList();
List<ExamQuestionsViewModel> exam = new List<ExamQuestionsViewModel>();
foreach (var IDs in exam)
{
currentCand = exam.CandID;
currentExam = exam.ExamId;
}
return (exam);
}
The error message I am getting is
'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>'
does not contain a definition for 'ExamId' and no extension method
'ExamId' accepting a first argument of type
'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>' could be found (are you missing a using directive or an assembly
reference?
What am I doing wrong here? All feedback much appreciated.
Please try this instead of your foreach:
foreach (var IDs in exam)
{
currentCand = IDs.CandID;
currentExam = IDs.ExamId;
}

EmitMapper (Flattened to Hierarchical)

I'd like to map from a flattened object to a hierarchical object based on a simple naming convention. For example:
public class FlatObject {
public string Name__FirstName { get; set; }
public string Name__MiddleName { get; set; }
public string Name__LastName { get; set; }
}
public class HierarchicalObject {
public SubObject Name { get; set; }
}
public class SubObject {
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
The simple naming convention is the double-underscore within the FlattenedObject.
How would I accomplish this using EmitMapper?
EmitMapper cannot do this task without significant code changes.

Linq query to select items in list from another list

I have a dictionary and a list.
AllMeta: is a dictionary<string, WikiMeta>
Meta: is a list<WikiMeta>
public class WikiMeta
{
public string ContentTitle { get; set; }
public string PageTitle { get; set; }
public string PageMetaDescription { get; set; }
public List<WikiArticle> Articles = new List<WikiArticle>();
public List<WikiGroup> Groups = new List<WikiGroup>();
}
public class WikiGroup
{
public string Name { get; set; }
}
I need to select values from AllMeta where AllMeta.Values.Group == Meta.Group returning a list of WikiMeta.
However, I'm struggling to construct the syntax (lambda), so any help would be appreciated.
try this:
List<WikiMeta> wikis= AllMeta.Values
.Where(allmeta =>
Meta.Any(meta=> meta.Group == allmeta.Group))
.ToList();
where I've assumed following:
public class WikiMeta
{
public string Name { get; set; }
public string Group { get; set; }
}
public IDictionary<string,WikiMeta> AllMeta { get; set; }
public List<WikiMeta> Meta { get; set; }
make sure you referenced System.Linq namespace i.e.
using System.Linq;

Cannot apply indexing with [] to an expression of type ICollection

Can someone please provide code to fix this error?
"Cannot apply indexing with [] to an expression of type 'ICollection'
Essentially, I'm trying to save/bind a value from a collection of objects.
#model MVC3.Models.Parent
#Html.EditorFor(model => model.Bs[0].Val)
public class A
{
public int Name { get; set; }
public virtual ICollection<B> Bs { get; set; }
}
public class B
{
public int Val { get; set; }
public virtual A A { get; set; }
}
ICollections are not ordered, so that cannot be indexed.
Instead, you should use a separate ViewModel class with IList<T> property.
Use IList
public class A
{
public int Name { get; set; }
public virtual IList<B> Bs { get; set; }
}

Resources