This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
What is the correct way to do something like this.
db.Tasks.Where(t => {t.CategoryId == 1 || t.CategoryId == 2) || t.CategoryId == 3)}).ToList();
Your brackets are just wrong at the moment, both in terms of curly braces and plain parentheses. You don't actually need any brackets within the expression - this is fine (reformatted for clarity):
var list = db.Tasks
.Where(t => t.CategoryId == 1 ||
t.CategoryId == 2 ||
t.CategoryId == 3)
.ToList();
A lambda expression is exactly that – it consists of a single ordinary expression. (such as a || b || c)
Braces are for statements, not expressions.
Just remove the {} and that will work fine.
You also have some stray ).
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
It was my understanding that C# conditional operators perform short circuiting. However, my program throws an exception while running this code. Index is equal to -1, so the second condition should never execute, but I get an index out of bounds exception.
if (index != -1 || pieces[index].Type != PieceType.Rook)
{
allowed = false;
}
You have used || so when the first condition fails (index IS -1) the runtime needs to check the second condition before excluding the code from execution and that will trigger the exception,
Instead, you use && (AND) if you want to enter the if only when the two conditions are true.
In this way, as before, the runtime checks the first condition and now getting the false result is enough to decide that there is no need to check the second condition..
if (index != -1 && pieces[index].Type != PieceType.Rook)
{
allowed = false;
}
Replace || with &&
if (index != -1 && pieces[index].Type != PieceType.Rook)
Otherwise the second codition is evaluated if the first is false what you don't want. You want both conditions being true and especially the first one.
That's because you use OR operator. First condition is false, so second starts evaluating.
You should have index != 1 && ....
Also, is index < -1 or >= pieces.Length?
|| will stop evaluating when it finds that something is true. Since index != -1 is false it will evaluate both sides of the expression. If you && it would stop once it finds a false. I would recommend reading up on lazy evulation.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am working on a linq method and cannot seem to get the return type to match the method signature. Any guidance would be much appreciated.
Thanks!
private static IEnumerable<KeyValuePair<string, string>> RunQuery(XDocument doc)
{
var data = from b in doc.Descendants("Company")
where b.Attribute("name").Value == "CompanyA"
from y in b.Descendants("Shirt")
from z in y.Descendants("Size")
select new
{
color = y.Attribute("id").Value,
price = z.Value
};
return data;
}
You have to create KeyValuePairs.
...
select new KeyValuePair(y.Attribute("id").Value, z.Value)
You can make your query much sorter. Note I've removed from y in b.Descendants("Shirt"), because Descendants parse the whole xml node including all it's descendants up to lowest level. This query will return Dictionary<string, string>, which implements IEnumerable<KeyValuePair<string, string>>, so you don't need to change method signature, but I strongly recommend to do this, because client's won't be able to access dictionary elements with constant time
return doc.Descendants("Company")
.Where(node => node.Attribute("name").Value == "CompanyA")
.Descendants("Size")
.ToDictionary(node => node.Attribute("id").Value,
node => node.Value);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm porting a c++ class to C# and i have a difficulty.
I would like to find an equivalent of SpanIncluding.
Here is my cpp code :
while (Notes.Mid(j,1).SpanIncluding("0123456789").IsEmpty()!=NULL){}
Anyone can help me please ?
I believe SpanIncluding starts matching from the start of the string, stopping when the first non-matching character is found.
So one formulation in the general case would be this:
string match = new string(someString.ToCharArray().
TakeWhile(c => "0123456789".Contains(c)).ToArray());
(or an equivalent using a regular expression).
However, in the example given in the question there's only one character so the whole thing probably boils down to a test of whether this character is >= '0' and <= '9':
while(char.IsDigit(Notes[j])) { ... };
I found the MSDN page for SpanIncluding, and it seems like a ridiculously specific function. I can't really understand what it tries to solve, since it has some strange caveats.
LINQ would be one way of implementing it:
string text = "2334562";
IEnumerable<char> spannedChars = text.TakeWhile(c => "1234567890".Contains(c));
This is a more direct port of SpanIncluding than queen3's option, if I understand the MSDN page correctly, because the result set should stop the minute it hits a character not in the spanning string.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need to convert a char to an int and back.
Here's what I'm using to convert the char to an int:
(char)65 // Returns 'a'
Here's what I'm TRYING to use to convert it back:
(int)'a' // Returns 97
Any ideas what I can do?
try this,
char x = 'a';
int y = (int)x;
65 is the character code for a capital 'A'. 97 is a lower case 'a'.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What I need is to order IQueriable (the result of LINQ to SQL method) and return first count elements if count is defined. I use the following code:
IOrderedQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0) list = list.Take(count).OrderByDescending(item=>item.Time);
It works fine, but I don't like calling OrderByDescending twice. Can I make it any better without breaking the order and also without double-ordering?
There is absolutely no need for the second OrderByDescending. Simply remove it:
IQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0)
list = list.Take(count);