Linq query to select items in list from another list - linq

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;

Related

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

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\"")

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;
}

Joining in LINQ to select a sublist within a list

I have two classes as follows:
public class HRM_SERVICE_PERD_BNFT_DTLModel
{
public string SRVC_PERD_BNFT_CODE { get; set; }
public string RTR_BNFT_CODE { get; set; }
public string RTR_BNFT_NAME { get; set; }
public string RTR_BNFT_CALC_MODE { get; set; }
public string SAL_HEAD_CODE { get; set; }
public string SAL_HEAD_NAME { get; set; }
public string RTR_BNFT_IN_PERCENT { get; set; }
public string RTR_BNFT_AMOUNT { get; set; }
public string RTR_BNFT_INACTV_DATE { get; set; }
public short? RTR_BNFT_SLNO { get; set; }
}
public class HRM_RETIREMENT_BENEFITModel : BaseModel
{
public string RTR_BNFT_CODE { get; set; }
public string RTR_BNFT_NAME { get; set; }
public string RTR_BNFT_SRTNM { get; set; }
public string RTR_BNFT_REM { get; set; }
public short? RTR_BNFT_SLNO { get; set; }
}
Now I want to select data from the first model for a specific 'SRVC_PERD_BNFT_CODE', then I need to select the 'RTR_BNFT_NAME' from the second table for all the filtered data along with some other value from the first model. I'm trying kind of joining, but not getting the name. It's showing system.collection.string instead of value.
Here is what I'm trying so far:
public List<HRM_SERVICE_PERD_BNFT_DTLModel> GetBenefitData(string mstcode)
{
var model2 = DataContext.HRM_SERVICE_PERD_BNFT_DTL.AsEnumerable().Where(m => m.SRVC_PERD_BNFT_CODE == mstcode).Select(s=>s).ToList();
var model = DataContext.HRM_SERVICE_PERD_BNFT_DTL.AsEnumerable().Where(m => m.SRVC_PERD_BNFT_CODE == mstcode)
.Select(s => new HRM_SERVICE_PERD_BNFT_DTLModel
{
RTR_BNFT_CODE = s.RTR_BNFT_CODE,
RTR_BNFT_SLNO = s.RTR_BNFT_SLNO,
RTR_BNFT_CALC_MODE = s.RTR_BNFT_CALC_MODE,
RTR_BNFT_AMOUNT = (s.RTR_BNFT_AMOUNT).ToString(),
RTR_BNFT_NAME = (from x in model2
join c in DataContext.HRM_RETIREMENT_BENEFIT on x.RTR_BNFT_CODE equals c.RTR_BNFT_CODE into p
from b in p.AsEnumerable()
select b.RTR_BNFT_NAME).ToList().ToString(),
RTR_BNFT_IN_PERCENT = (s.RTR_BNFT_IN_PERCENT).ToString()
}).ToList();
return model;
}
What I'm doing wrong? Please help.
Following is suspicious.
.ToList().ToString()
Remove the ToString part and replace it with something like FirstorDefault, then it should work.
Reason for getting something like System.Collection.String is that List<T>.ToString() is usually typename until it is overridden in some way.
Following minimal snipped produce System.Collections.Generic.List1[System.String]`
List<String> list = new List<String>();
var str = list.ToString();
Console.WriteLine(str); //System.Collections.Generic.List`1 [ System.String ]

Using Linq to populate a class

I am getting a weird behavior. I have a class that I created that is used to format data comping from a data entity into a data grid. I am a using a linq query to create a list of the class type from a list of the entity type. Some of the properties of the class are accessible from the linq query but other give me an error. (AMNotStartedPortalDisplay' does not contain a definition for 'ChecklistStatusID'). So my question is why can linq access some properties but not others? I see no reason why this should be happening.
Here is my class:
public class AMWOTPortalDisplay
{
public string DisplayName { get; set; }
public string LOB { get; set; }
public string DisplayProjectPackages { get; set; }
public string ChecklistStatus { get; set; }
public int ChecklistStatusID { get; set; }
public string InstallDate { get; set; }
public string dateToYellow { get; set; }
public string dateToRed { get; set; }
public string ApplicationManager { get; set; }
public string ApplicationManagerLanID { get; set; }
public int ApplicationManagerUserID { get; set; }
public string ImpersonatedManager { get; set; }
public string ImpersonatedManagerLanID { get; set; }
public int ImpersonatedManagerUserID { get; set; }
public string DelegateName { get; set; }
public string DelegateLanID { get; set; }
public int DelegateUserID { get; set; }
public string WOTAssignee { get; set; }
public int ChecklistID { get; set; }
public string DisplayLinkText { get; set; }
public string LinkTextURL { get; set; }
public string rowColor { get; set; }
public string rowTextColor { get; set; }
}
And here is the linq query as I have it so far:
var portaldisplay = checklists
.Select(c => new AMNotStartedPortalDisplay
{
DisplayName = string.Format("{0} ({1})", c.Application.Name, c.Application.ApplicationID),
LOB = c.Application.LOB,
ChecklistStatus = c.ChecklistStatusType.TypeName,
ChecklistStatusID = c.ChecklistStatusTypeID
});
Thanks,
Rhonda
Be careful with your types:
public class AMWOTPortalDisplay
And then:
Select(c => new AMNotStartedPortalDisplay { ... })
It looks like your query should probably be:
Select(c => new AMWOTPortalDisplay { ... })

Resources