I am trying to remove from a list in a list where there are null.
For Example:
responses.Questions[0].Options[0].Value = "asdf";
responses.Questions[0].Options[1].Value = null;
responses.Questions[0].Options[2].Value = 1;
I want to remove the second options in the list because the value is null. So When I am done I have a list like so:
responses.Questions[0].Options[0].Value = "asdf";
responses.Questions[0].Options[1].Value = 1;
I tried the code below but it doesn't appear to work:
responses.Questions.Select(q => q.Options.RemoveAll(o => o.Value == null));
use foreach:
foreach(var q in responses.Questions)
{
q.Options.RemoveAll(o => o.Value == null);
}
Try this
responses.Questions.ForEach(q => q.Options.RemoveAll(o => o.Value == null));
Use a Where clause in order to exclude null option lists:
responses.Questions
.Where(q => q.Options != null)
.ForEach(q => q.Options.RemoveAll(o => o.Value == null));
(According to one of your comments that is deleted now, you got an exception because of Options being null.)
Note: null values can appear at different levels here. responses, Questions, q and Options could theoretically all be null. Add tests where appropriate.
Related
I need to remove a particular value from the list based on the condition
skillResult.Where(search =>
resrictedskills.Any(resrictedskill =>
search.L5_Id.ToString() == resrictedskill.CRTS_SKILLID.ToString()
&& resrictedskill.CRTS_SKILLTYPE.ToLower() == "primary"
)
).ToList()
.ForEach(skill => skill.isRestrictedpri = true);
I tried using remove but I am getting error "cannot convert from void to particular list model"
skillResult.Remove(
skillResult.Where(search =>
resrictedskills.Any(resrictedskill =>
search.L5_Id.ToString() == resrictedskill.CRTS_SKILLID.ToString()
&& resrictedskill.CRTS_SKILLTYPE.ToLower() == "primary"
)
).ToList()
.ForEach(skill => skill.isRestrictedpri = true)
);
Remove() expects one item and returns a bool telling whether the item was found and could be removed.
You must use RemoveAll which expects a condition and returns and int telling how many items have been removed.
Since none of the two methods return skill objects, you cannot chain the ForEach on them to set a property of the skills. Therefore, you need two statements.
skillResult.RemoveAll(skillResult => resrictedskills
.Any(restrictedSkill =>
skillResult.L5_Id == restrictedSkill.CRTS_SKILLID &&
restrictedSkill.CRTS_SKILLTYPE.ToLower() == "primary"
)
);
skillResult.ForEach(skill => skill.isRestrictedpri = true);
I have a linq query.In here when item.nValue sometimes comes as a empty string.I need to check if nValue comes as a empty string article value need to show "OLD RECORD".
I want to do this in this linq query..
var article = _newsList.Where(e => e.Id == Guid.Parse(item.nValue)).Select(e => e.NewsName).FirstOrDefault();
item.nValue variable is not from LINQ collection so you can try:
var article = "OLD RECORD";
if (Guid.TryParse(item.nValue, out Guid articleId))
{
article = _newsList.Where(e => e.Id == articleId).Select(e => e.NewsName).FirstOrDefault();
}
If I understand your question,So
var article = _newsList.Where(e => e.Id == Guid.Parse(item.nValue)).DefaultIfEmpty(yourDefaultValue).Select(e => e.NewsName).FirstOrDefault();
probably DefaultIfEmpty can help you.Please test it.
Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty
var article = _newsList.Where(e => e.Id == (item.nValue==""?Guid.Parse(item.nValue):"OLD RECORD"))
.Select(e => e.NewsName).FirstOrDefault();
I have a problem that I can't fix with my rather small LINQ knowledge.
I have a collection of lists that contain a list of fields.
I need only the lists with the properties hidden == false and that got fields with the description "Special Field".
I tried the following approaches... none of them worked:
clientContext.Load(listCollection,
lists => lists
.Where(list => list.Hidden == false)
.SelectMany(list => list.Fields)
.Where(field => field.Description == "Special Field"));
and
var listQuery = from list in listCollection.Where(l => l.Hidden == false)
from field in list.Fields
where field.Description == "Special Field"
select list;
and
var listQuery2 = listCollection
.SelectMany(lists => listCollection)
.Where(l => l.Hidden == false)
.SelectMany(fields => fields.Fields)
.Where(f => f.Description == "Special Field"
all followed by
var result = clientContext.LoadQuery(listQuery2);
clientContext.ExecuteQuery();
None of them worked.
I get the following exception (for the last query but message is similar to the other querys):
The query expression 'value(Microsoft.SharePoint.Client.ListCollection).SelectMany(lists => value(docma.Library.Classes.SharepointDataConnector+<>c__DisplayClass56_0).listCollection).Where(l => (l.Hidden == False)).SelectMany(fields => fields.Fields)' is not supported.
Does anybody clue what I am doing wrong or how to get it to work?
Do I need to use 2 queries?
Performance is important.
Thanks in advance.
As the error says Enumerable.SelectMany is not supported by LINQ to SharePoint provider
To retrieve the following data
I need only the lists with the properties hidden == false and that got
fields with the description "Special Field".
you could utilize the following query via ClientContext.LoadQuery method:
var lists = ctx.Web.Lists;
var result = ctx.LoadQuery(lists.Where(list => list.Hidden == false).Include(l => l.Fields.Where(f => f.Description == "Special Field")));
ctx.ExecuteQuery();
or
var lists = ctx.Web.Lists;
var result = ctx.LoadQuery(lists.Where(list => list.Hidden == false).Include(l => l.Title,l => l.Fields.Where(f => f.Description == "Special Field").Include( f => f.Title, f => f.Description)));
ctx.ExecuteQuery();
where you could specify what properties needs to be returned like List.Title, Field.Title and Field.Description is this case
Update
To return only lists which contains a specific field, the following filter could be applied:
var foundLists = result.Where(l => l.Fields.Count > 0).ToList();
That My Linq query
var result = db.APPLICATIONS
.Where(a => Statuses.Contains(a.STATUS_ID))
.Where(a => a.TrackingNo == TrackingNo)
Statuses is a int list and TrackingNo is a nullable int (int?).
Problem:
If the TrackingNo is null then i dont want to run this clause or just skip this condition.
LINQ queries can be built in multiple steps:
var result = db.APPLICATIONS
.Where(a => Statuses.Contains(a.STATUS_ID));
if (TrackingNo != null)
{
result = result.Where(a => a.TrackingNo == TrackingNo);
}
Note that if you have a Select (a projection), you probably must build the query in multiple steps in multiple variables:
var result2 = result.Select(a => new { a.STATUS_ID });
with the result2 "built" after the if.
You can check a nullable int by using its "HasValue" property.
var result = db.APPLICATIONS
.Where(a => Statuses.Contains(a.STATUS_ID))
.Where(a => a.HasValue && (a.TrackingNo == TrackingNo))
This will cause it to evaluate the "HasValue" prior to checking the value itself. If HasValue return false, then it will never evaluate the rest of the expression (and thus not cause NullReferenceException).
If it is of type "int?", then this will work.
Just add && condition and check null. And you can use 1 where condiiton here why second where.Pls try this:
var result = db.APPLICATIONS
.Where(a => Statuses.Contains(a.STATUS_ID)
&& a.TrackingNo!=null
&& a.TrackingNo == TrackingNo)
You should first check the values of the filtering parameters before trying to add more stuff to the store expression. This would only apply the Statuses and TrackingNo filtering if the nullable TrackingNo has a value. Otherwise it will return all APPLICATIONS as IQueryable.
var result = db.APPLICATIONS.AsQueryable();
if (TrackingNo.HasValue)
{
result = result.Where(a => Statuses.Contains(a.STATUS_ID) && a.TrackingNo == TrackingNo);
}
return result;
Alternatively, this would check if you have any statuses to apply and the tracking separatedly.
var result = db.APPLICATIONS.AsQueryable();
if (Statuses != null && Statuses.Count() > 0)
{
result = result.Where(a => Statuses.Contains(a.STATUS_ID));
}
if (TrackingNo.HasValue)
{
result = result.Where(a => a.TrackingNo == TrackingNo);
}
return result;
Or third option, as it is unclear what you really wanted. This would apply the statuses filtering always and tracking only if it is available
var result = db.APPLICATIONS.Where(a => Statuses.Contains(a.STATUS_ID));
if (TrackingNo.HasValue)
{
result = result.Where(a => a.TrackingNo == TrackingNo);
}
return result;
I have a 2 collection of diff types. i want to match a string in those collection and return the collection which did not match.
1) ac_CategoryList
2) mw_CharityList
would like to match if ac_CategoryList.Title is there in mw_CharityList.EntryTitle. if it is not there, than return the ac_CategoryList collection items which are not matched. and return one more collection of mw_CharityList type which matched in ac_CategoryList.Title. because i need to update the status in mw_CharityList collection.
var var charityList = _db.mw_CompetitionsEntry.Where(e => e.IsInvalid == false && e.IsPublished).ToList(); // first get the entire valid collection
var categoryList = _db.ac_Category.Where(c => c.Title != null && c.IsDeleted == false).ToList(); // get the entire valid collection
var titleNotExitsCollection = categoryList.Where(c => charityList.Any(e => e.EntryTitle.Trim() != c.Title.Trim())).ToList();
var titleExitsCollection = charityList.Where(e => categoryList.Any(c => c.Title.Trim() == e.EntryTitle.Trim())).ToList();
right now titleNotExitsCollection and titleExitsCollection returns the same no of records. i dont know what i am doing wrong... please help
Looks like there is a not operator missing, try:
var titleNotExitsCollection = categoryList.Where(c => !charityList.Any(e => e.EntryTitle.Trim() == c.Title.Trim())).ToList();
var commonTitles = categoryList.Select(x=>x.Title.Trim())
.Intersect(charityList.Select(x=>x.EntryTitle.Trim()));
var titleNotExitsCollection = categoryList.Where(x=>!commonTitles.Contains(x.Title))
.ToList();
var titleExitsCollection = charityList.Where(x=>commonTitles.Contains(x.EntryTitle))
.ToList();