I want to check if a variable of type guid exist. I used
new Db().JobCreate.Any(a => a.GuidVariable1.Equals(GuidVariable2, StringComparison.OrdinalIgnoreCase));
but I get the error Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead
How can I fix this?
I'm assuming you have another instance variable named GuidVariable2.
Unless there's something else involved I'd just do the following:
new Db().JobCreate.Any(a => a.GuidVariable1 == GuidVariable2);
If the variables are actually strings I'd do the following:
new Db().JobCreate.Any(a => a.GuidVariable1.ToLower() == GuidVariable2.ToLower());
Update based on comment:
Guid represents a hexidecimal number, so the case of the alpha digits is irrelevant. When represented as a string, case could be Upper, Lower or a combination but the actual number is the same:
var guid1 = Guid.Parse("a0449976-604e-4bdf-826d-234c4564c3e0");
var guid2 = Guid.Parse("A0449976-604E-4BDF-826D-234C4564C3E0");
var guid3 = Guid.Parse("A0449976-604E-4bdf-826d-234c4564c3e0");
guid1 == guid2; //true
guid2 == guid3; //true
Here you go:
new Db().JobCreate.Any(a => string.Equals(
a.GuidVariable1, GuidVariable2, StringComparison.OrdinalIgnoreCase));
(this is of course assuming your so-called GUIDs are actually strings, if you want to compare GUIDs just do this:)
new Db().JobCreate.Any(a => a.GuidVariable1.Equals(GuidVariable2));
Related
Why can we use a code like this:
let student = {name:"John", surname:"Doe", index:386754};
let text = "";
let x;
for (x in student) {
text += student[x] + " "; }
And it would preview: John Doe 386754.
But when I formulated it like this:
let student = {name:"John", surname:"Doe", index:386754};
let text = "";
let x;
for (x in student) {
text += student.x + " "; }
, it returnes: undefined undefined undefined.
I suppose it's a pretty basic thing, but I had to ask because I can't find an appropriate answer.
Thank you ahead!
You should check out the data structures. You create a hash table using the variable student. So you can call inner variables (key-value pairs) by using brackets as you did student[name]. The second one student.name means you are calling a method of a class, which you don't have.
I recommend you to check what data structures exist, and how to use them.
The usage of object.something vs object[something] varies in different languages, and JavaScript is particularly loose in this aspect. The big difference here is that in object[something], something must reference a string corresponding to a key in object. So if you had something = 'myKey', and myKey was the name of a key in something (so object = {'myKey': 'value', ...}), you would get value. If you use object.something, you are asking JavaScript to look for a key in object with the name something. Even if you write something = 'myKey', using a dot means that you are looking within the scope of the object, making variables in your program effectively invisible.
So when you write student.x, you get undefined because there is no key 'x': 'value' in student for your program to find. Defining x as a key in your for loop does not change this. On the other hand, writing student[x] means that your program is finding the value x is referencing and plugging it in. When x is 'name', the program is actually looking for student['name'].
Hope that clarifies your issue. Good luck!
UserList is a list of dictionaries, like:
[
{Name:"Alex",Age:25},
{Name:"Peter",Age:35},
{Name:"Muhammad",Age:28},
{Name:"Raul",Age:29}
]
RowColorList is a list of colors: [#bcf,#fc0]
The new UserList should contain one RowColor for every name, taken in sequence from RowColorList:
[
{Name:"Alex",Age:25,RowColor:#bcf},
{Name:"Peter",Age:35,RowColor:#fc0},
{Name:"Muhammad",Age:28,RowColor:#bcf},
{Name:"Raul",Age:29,RowColor:#fc0}
]
I tried the following code:
UserList.Zip(RowColorList,(user,color) => user.Add("RowColor",color))
With this code, the new UserList will only contain as many entries as are in RowColorList. I would like him to start from the beginning of RowColorList again, whenever the available colors are used up. How?
You can create a function to return an infinite enumerable of Color / string (or whatever the type of RowColor is), by using yield return as a lazy generator:
public IEnumerable<Color> InfiniteColors()
{
while (true)
{
foreach (var color in RowColors)
{
yield return color;
}
}
}
This can then be used with any of the Linq IEnumerable extension methods such as Zip.
UserList.Zip(InfiniteColors(),(user,color) => user.Add("RowColor",color))
Edit - Explanation
The reason why InfiniteColors doesn't hang is because the state machine will yield back to the caller after each result, and Zip will terminate on the first enumerable to complete, which is because the other collection being zipped is finite (i.e. UserList)
Obviously you shouldn't try and Zip the InfiniteColors enumerable with itself, nor should you try and materialize InfiniteColors, i.e. don't call InfiniteColors.ToList() or such :-):
Something like this should do the trick:
var i = 0;
var l = RowColorList.Count;
UserList.ForEach(user => user.Add("RowColor", RowColorList[(i++) % l]));
The % operator will guarantee "cyclic" access to the RowColorList.
When I have a nullable array/list/hashmap such as
var x: ArrayList<String>? = null
I know can access the element at index 1 like so
var element = x?.get(1)
or I could do it in a unsafe way like this
var element = x!![1]
but why can't I do something like this
var element = x?[1]
what's the difference between getting elements from an array using the first example and the last example, and why is the last example not allowed?
In the first example, you're using the safe call operator ?. to call the get function with it.
In the second example, you're using the [] operator on the non-nullable return value of the x!! expression, which of course is allowed.
However, the language simply doesn't have a ?[] operator, which would be the combination of the two. The other operators offered are also don't have null-safe variants: there's no ?+ or ?&& or anything like that. This is just a design decision by the language creators. (The full list of available operators is here).
If you want to use operators, you need to call them on non-nullable expressions - only functions get the convenience of the safe call operator.
You could also define your own operator as an extension of the nullable type:
operator fun <T> List<T>?.get(index: Int) = this?.get(index)
val x: ArrayList<String>? = null
val second = x[2] // null
This would get you a neater syntax, but it hides the underlying null handling, and might confuse people who don't expect this custom extension on collections.
Let us have the following:
Func<string, int> counterOfChar = (myString) => {
Console.WriteLine("Here is my parameter "+myString);
return myString.Count();
};
I want to bring all expressions involved here , by defining them so:
Expression<Action<string>> first = (param) => Console.WriteLine("Here is my parameter "+param);
Expression<Func<string, int>> second = (param) => param.Count();
And then call Expression.Block(first, second); as an example .
I am struggling for a week now and I don't want to tell you how diverse are the errors received until this moment.
Can someone write the corresponding Block and lambda expression for the delegate, but not going deep into ex: Method.Call ? Just stick to expressions !?
Thank you!
Expression<Action<string>> first = (param) => Console.WriteLine("Here is my parameter " + param);
Expression<Func<string, int>> second = (param) => param.Length; // .Count() is useless here!
Expression<Func<string, int>> third =
Expression.Lambda<Func<string, int>>(
Expression.Block(first.Body,
Expression.Invoke(second, first.Parameters[0])),
first.Parameters[0]);
var f = third.Compile();
var r1 = f("Hello");
"merging" two Expressions is always a little complex, because the two param of the two expressions are "different". They aren't the same param (it's like one is param1 and the other is param2). Here we resolve it by reusing the first parameter of the first expression as the parameter of the "new" expression and Expression.Invoke the other expression.
Without cheating, we could have
var par = Expression.Parameter(typeof(string));
Expression<Func<string, int>> third =
Expression.Lambda<Func<string, int>>(
Expression.Block(
Expression.Invoke(first, par),
Expression.Invoke(second, par)),
par);
var f = third.Compile();
var r1 = f("Hello");
where we introduce a new parameter par and we Expression.Invoke the other two expressions.
Note that Entity Framework doesn't support Expression.Invoke. In this case you can use a parameter rewriter (something like this.)
I have a list with objects of x type. Those objects have an attribute name.
I want to find if a string matchs any of those object names. If I would have a list with the object names I just would do if string in list, so I was wondering given the current situation if there is a way to do it without having to loop over the list.
any(obj for obj in objs if obj.name==name)
Note, that it will stop looping after first match found.
Here's another
dict( (o.name,o) for o in obj_list )[name]
The trick, though, is avoid creating a list obj_list in the first place.
Since you know that you're going to fetch objects by the string value of an attribute, do not use a list, use a dictionary instead of a list.
A dictionary can be trivially "searched" for matching strings. It's a better choice than a list.
What do you want to do if the string matches? Do you just want to return True/False, or return a list of objects that match?
To return a boolean:
any(obj.name == name for obj in objs)
(I find this slightly more readable than Denis Otkidach's version).
to filter the list:
[obj for obj in objs if obj.name == name]
if string in [x.name for x in list_of_x]
for i in listOfItemsOfTypeX:
if i.name == myString: return True
return False