I have resData object with Courses listItem and Error property. I have checked all the Courses list item Error properties and all of them are null.
However, when I check in the following, it returns true.
I wonder what I am missing?
if(resData.Courses.Select(x => x.Error != null && x.Error.Length > 0).Count() > 0)
{
Console.WriteLine("Error");
}
It's because you are using a Select, you are projecting your collection, not applying a condition, you should use Where instead, or use an overload of Count method:
if(resData.Courses.Count(x => x.Error != null && x.Error.Length > 0) > 0)
And as #Chris pointed out, it's better if you use Any to avoid enumerate the entire list:
if(resData.Courses.Any(x => x.Error != null && x.Error.Length > 0))
Related
I have this code:
if (App.selectedPhrases != null && App.selectedPhrases.Count != 0)
Is there a way I can simplify that using the ? operator to check for null rather than have two different checks with an && joining them?
You can use the null conditional member access operator (?.) to conditionally access a member only if it is not null, like so
if (App.selectedPhrases?.Count > 0)
I decided to use > instead of != as the Count of a collection cannot be smaller than 0, and null != 0 is true, which is incorrect in our case
I'd go with this:
// So if it's null, then return 0 so that the second check will not be true
if ((App.selectedPhrases?.Count ?? 0) != 0) {
// Do stuff
}
Try this:
if (App.selectedPhrases?.Count > 0)
Or:
if (!(new List<int?>{null, 0}.Contains(App.selectedPhrases?.Count)))
I have a List property that I am setting like so:
testCard.LstSummaries =
db.Summaries.Where(
x =>
(x.AID == aId || x.AInformation.RegNumber == aRegNumber) && DbFunctions.TruncateTime(x.Day) == DateTime.Today.Date &&
x.deleted == false).ToList();
Then I have a conditional statement:
if (testCard.LstSummaries.Count > 0)
{
if (
testCard.LstSummaries.All(
x =>
(x.AID == aId || // ERROR HAPPENS ON THIS LINE
x.AInformation.RegNumber == aRegNumber) &&
DbFunctions.TruncateTime(x.Day) == DateTime.Today.Date && x.deleted == false))
{
// .... do something
}
I get an error:
This function can only be invoked from LINQ to Entities.
I want to avoid to make multiple calls to the database.. furthermore testCard.LstSummaries already has the values I am looking for.. but if I do this:
if (testCard.LstSummaries.Count > 0)
{
if (
db.Summaries.All(
x =>
(x.AID == aId || // NO ERROR
x.AInformation.RegNumber == aRegNumber) &&
DbFunctions.TruncateTime(x.Day) == DateTime.Today.Date && x.deleted == false))
{
// .... do something
}
I feel like making this call to the database is pointless because I would be retrieving the same results that are already stored in testCard.LstSummaries, but I can't invoke .All() because it's not LINQ to Entities.
Is there a workaround for this?
Problem is with DbFunctions.TruncateTime(x.Day), because it is converted to sql on runtime. Try to check without it.
I have list of items defined as: IQueryable and I need go trough and find if any of the fields contain a specific word. When I use foreach and loop trough it and use .Contains, then everything works fine. The problem is that I need to use LINQ and .Boost, because some of the fields have higher priority when I find words in them.
When I use LINQ I get no results. For most languages LINQ works fine, but for Japanese words it does not. Can someone explain me why foreach with .Contains works fine and LINQ with .Where does not?
Code example:
foreach (var item in itemList)
{
if ((item.PageHeadings != null && item.PageHeadings.Contains(keyword)) ||
(item.PageName != null && item.PageName.Contains(keyword)) ||
(item.MetadataDescription != null &&
item.MetadataDescription.Contains(keyword)) ||
(item.MetadataKeywords != null &&
item.MetadataKeywords.Contains(keyword)) ||
(item.ExpandedContent != null &&
item.ExpandedContent.Contains(keyword)))
{
list.Add(item.Name);
}
}
LINQ example:
list.AddRange(from item in itemList where (item.PageHeadings != null
&& item.PageHeadings.Contains(keyword).Boost(BoostPageHeading))
|| (item.PageName != null && item.PageName.Contains(keyword).Boost(BoostPageName))
|| (item.MetadataDescription != null && item.MetadataDescription.Contains(keyword).Boost(BoostMetadataDescription))
|| (item.MetadataKeywords != null && item.MetadataKeywords.Contains(keyword).Boost(BoostMetadataKeywords))
|| (item.ExpandedContent != null && item.ExpandedContent.Contains(keyword).Boost(BoostExpandedContent)) select item.Name);
Thanks
UPDATE:
Example of phrase: 天窓使用例
So I have a list of orders...and if the list is empty I want to to jump to the 'else.'
But because its a list (even if its empty) there still is an instance of it? right? anyway if there are no orders it still goes into the if statement so I tried adding .count == 0...
but it still goes into the if statement...what do I need to say where if there are no actual records in orders go to 'else....thanks for any replies
IEnumerable<OrderRecord> orders = _orderService.GetOrdersByCustomer(id, OrderStatus.Completed).ToArray();
if (orders != null && orders.Count() == 0 )
{
//order exists
}
else
{
//no order
}
You want to check if there are more than 0 items in the list
if (orders != null && orders.Count() > 0 )
{
//order exists
}
else
{
//no order
}
The list itself doesn't count as an item.
Or, as suggested by Richard Ev in the comments, you can use orders.Any which will return true if there are elements in the list.
You need to change your condition
orders.Count() == 0
To
orders.Count() > 0
since currently its check if the list contains no record.
You can also try Enumerable.Any like:
if(orders != null && order.Any())
See why its better to use Any in place of Count()
I assume you want to check for > 0 instead
if (orders != null && orders.Count() > 0 )
{
//order exists
}
or orders.Any()
Let's say you have a List l_mur = new List();
And you populate the list.
Then based on conditions you want to REMOVE some values without requerying...
l_mur.RemoveAt(l_mur.FindIndex(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid)));
However, the code I used here, does not seem to work. It tells me index out of range, but how can it be out of range if I am just searching for something that truly does exist.
List<T>.FindIndex() returns -1 in case there is no match found - which is out of range for List<T>.RemoveAt().
Also note that FindIndex() only returns the index of the first occurrence based on your predicate - if there is more than one match you will only be able to delete the first one of them with your current approach.
A better approach to delete in place based on a predicate would be RemoveAll():
l_mur.RemoveAll(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid));
May be a good idea is to filter the list to a new instance of the list:
var l_mur = l_mur.Where(f => (f.xid != tmur.xid || f.sid != tmur.sid || f.mid != tmur.mid || f.bid != tmur.bid));
Use this code:
l_mur.Remove(l_mur.Find(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid)));