Hey I have been using bencodeNET for a while and it works great with one file torrents but when it comes to multiple file torrents it stores the filenames in a list, this shouldn't be a problem but it seems to be.
dosent matter how I try to read from the list I always end up with a error
string search = "5";
string result = torrent.Files.Single(se => se == search);
Operator '==' cannot be applied to operands of type 'TorrentFile.TorrentFileFileEntry' and 'string'
https://github.com/Krusen/BencodeNET/blob/master/BencodeNET/Torrents/TorrentMultiFileInfo.cs
I do feel that the answer is here but cant figure it out how it should be implemented, thanks for any help
se is an object of type TorrentFileFileEntry, which you are trying to compare to a string - this won't work as they are objects of different types.
Perhaps you should be searching a property of se, e.g. se => se.FileName == search.
I'm guessing the property name here.
Related
I have a column named 'Jul' which contains some values like '123' and '-'. I'm trying to get sum of column by adding values and neglecting '-'. I tried:
object julysum;
julysum = Convert.ToDouble(FormattedPurchaseSummaryTable.Compute("SUM(Jul)", "Jul != '-'"));
but i'm getting error: Cannot interpret token '!' at position 5.
How can i resolve this error?
There is no != operator.
You likely want <>.
Please read here for more information on what operators are available: https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?redirectedfrom=MSDN&view=netframework-4.7.2#operators
Blam's answer already pointed you towards the expression language. Having fixed the first issue, we need to fix the second which is that Jul is a string, not an integer (or float, etc). So we need to convert it as well.
But, per Compute I don't think it'll support a complex expression within the SUM to do the CONVERT.
So I think what you need to do is to add a new DataColumn with an expression something like:
IIF(Jul != '-',CONVERT(Jul,'System.Int32'),0)
And then compute your SUM over this new column instead.
I have been reading up Expression trees, and I think this is a good example to use them, still I can't seem to grasp how this would be done.
I have a set of strings that I want evaluated, they are all of the type:
exp == exp , or exp != exp , or exp (<,>,>=,<=) exp if exp is Numerical Type.
The exp do not need to check if they are valid I am fine with them blowing up if they are not.
My issue is, how to I parse to get the actual obj.
I want to pass a string like these below
Owner.Property.Field == 3;
or
Owner.Field == 3;
or
Owner.Method(1) == true
And get if the evaluation is true or not. MY issue is how do I travel down the "path" on the left and get the value?
I implemented a version with Reflection and string parsing, that somehow does the work - except for when we are using a method, and honestly its not that performant at all. I want this to be as performant as possible can get, and if possible give me a small explanation of how the expression works , so I can learn.
You can use code generation libraries like CodeDOM or Roslyn to generate Func that will do the evaluation.
For example, in Roslyn you can create a Session and set an object containing Owner as the Host object of the Session. than you can generate the code in the Session as you wish like the following:
Session session = ScriptEngine.CreateSession(objectContainingOwnerAsProperty);
bool result = session.Execute<bool>("Owner.Field == 8");
Now result will contain the evaluation result for your string without reflection nor string analysis.
I have a search criteria stored in a string:
string Searchstr = "(r.Value.Contains("PwC") || (r.Value.Contains("Canadian") && r.Value.Contains("thrive"))) || (r.Value.Contains("Banana") && r.Value.Contains("Gayle"))"
I want to use this in a If statement to check the values:
if(searchstr)
{
then do this....
}
but the if should have a searchstr as boolean.
How to convert this to boolean?
EDIT: The requirement is to give search criteria dynamically in a text box in the following format - "PwC OR (Canadian AND thrive)".
Which will be used to search an XML file.
Therefore I have loaded an XML file and want to have a Where condition in LINQ for which I need to use Dynamic LINQ but string is not allowed in that and also I have some braces to deal with.
Thinking of that I have taken the resultset from the XML(The tag value which i need to search)
var selectedBook = from r in document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
select r;
and would ideally like to try something like:
var query=selectedbook.Where(searchstr)
OR
if(searchstr){....then do this}
You will need to do a bit of work to make this happen, but it is possible.
You should have a look at the dynamic LINQ library. This allows you to specify LINQ conditions (and other clauses) as strings and execute them just like LINQ operators.
Start with the explanation on ScottGu's blog and follow the links:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
I'm assuming the string is going to reference only a specific set of objects (r or r.Value in this case, for example - or anything else you want, as long as you know it beforehand). If this is the case, then:
Create a delegate that takes the objects (that may be referenced) as parameters
and returns a bool, as you want.
Programmatically write a small C# source file in memory that defines the query
as the body of a method (with a fixed name, preferably) that conforms to the delegate specified above.
Use the CSharpCodeProvider class to compile an assembly
with your custom function that returns the bool you want.
Run the dynamically written and compiled code from your main program.
Well as you may guess it is not going to be straight forward but at the same time it is not as hard a problem as it seems
You can perform a few steps to get what you want:
Get the search expression as input (for e.g. "PwC OR (Canadian AND thrive)")
Write an extension method on XElement that returns true and takes the search criteria as input. You will then be able to use
var selectedBook = from r in
document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
where r.SatisfiesCriteria(searchCriteria)
select r;
Write a parser class that parses searchCritera and stores it in parsed format. (for e.g. you can convert it into postfix notation). This is quite easy and you can use standard algorithm for this. For your purpose OR, AND will be operators and PwC etc. will be operands. Parenthesis will get removed as part of parsing.
Now simply invoke this parser from with in your extension method and then evaluate the postfix expression you get. This again can be done through standard stack based evaluation. Infact it would be better if you parse the criteria once and then only evaluate in where. While evaluating you need to replace the operands with r.Value.Contains
It seems like a good scenario for http://scriptcs.net/
I'm not sure if "key value" is correct word for it as there are few formats I believe, what im talking about is http://pastebin.com/XJVx1dB5 this is format where 88 is Z etc. I hope im clear.
I have tried many things, but convert from string function of keys converter class is the only one that is remotely close. The problem is, it converts "x" to 88 as I wanted, however it fails to convert " " or "[" because it expects "SPACE"(string) and not a single space as char I believe.
I used it like this, maybe there is another(correct?) version of using it:
((int)kc.ConvertFromString(s))
So what I want to do is to get that chars of mine to code ones. How can I achieve this ?
Try something like:
var stringValue = " ";
var intValue = (int)stringValue[0];
A string is an 'array of chars' - so waht you need to do is get the char [in this case the first one] - and convert that to an int - in this case simply by casting.
If you want to do it with the enum, then you're going to have to write a manual system to convert " " to Keys.Space .
If you review your enum Keys carefully, you will see there is no value for [.
In fact, that character (on my keyboard) is reported as Oem4.
This code works:
var c = kc.ConvertFromString("Oem4");
However,
var c = kc.ConvertFromString("[");
Cannot work because [ is not a valid member of the enum you are converting from.
What do you want to do once you have converted the string? That may help guide a better answer to your question.
I need a way to Parse an string into an integer value in c#. The problem is the user chosses a string from a combo box which contains strings such as "AAAAA" or "5". That means only at run time it is known if the parameter is a real string or an string which can be parsed to an integer. I tried around with reflection and have the fitting Parameter object.
ParameterInfo p = ps[i];
Type t = p.ParameterType;
I don't know how to go on from there or if it even is possible. I can't use if else statments because the program is supposed to load other interfaces with new parameters as well. So I could handle the default ones with if else statmentes but when a new interface with new Methodinfos is loaded that dos not work anymore.
I'm not usre that understood all your constraints. However you can parse string by using Int32.TryParse in case target string is not necessarily valid.
Int32.TryParse whould help you with that
To parse a string into an integer, you can use Convert.ToInt32(string_var), or any of the other conversion methods. See here for more.