How can i query the following string, to extract information from it?
<Glyphs
Fill="#ff000000"
FontUri="\Documents\1\Resources\Fonts\547B9B52-1992-40E2-BBAA-0D815B2C6215.odttf" FontRenderingEmSize="10"
StyleSimulations="BoldItalicSimulation"
OriginX="20"
OriginY="1094.96"
UnicodeString="SomeString" />
For example, how can i read the OriginY value with LINQ?
The string you got looks a lot like XML. You might therefore want to try LINQ to XML. For instance, use the XDocument class:
var doc = XDocument.Parse(#"<Glyphs
Fill=""#ff000000""
FontUri=""...."" FontRenderingEmSize=""10""
StyleSimulations=""BoldItalicSimulation""
OriginX=""20""
OriginY=""1094.96""
UnicodeString=""SomeString"" />");
var fill = doc.Root.Attribute("Fill");
string s = #"<Glyphs
Fill=""#ff000000""
FontUri=""\Documents\1\Resources\Fonts\547B9B52-1992-40E2-BBAA-0D815B2C6215.odttf"" FontRenderingEmSize=""10""
StyleSimulations=""BoldItalicSimulation""
OriginX=""20""
OriginY=""1094.96""
UnicodeString=""SomeString"" />";
double val = (double)XElement.Parse(s).Attribute("OriginY");
Console.WriteLine(val);
Related
I have an array of string
var searchString = new string[] {"1:PS", "2:PS"};
and a large result string eg;
var largeString = "D9876646|10|1:PS^CD9876647100|11|2:PS"
how do I check if any of the options in searchString exist in the largeString?
I know it can be done via loop quite easily but I am looking for an other way around since I need to append the following as search clause in linq query.
You can use LINQ for it with a simple Any() call, like this:
var hasAny = searchString.Any(sub => largeString.Contains(sub));
However, this is as slow as a foreach loop. You can find the answer faster with a regex constructed from searchString:
var regex = string.Join("|", searchString.Select(Regex.Escape));
var hasAny = Regex.IsMatch(largeString, regex);
Depending on the nature of your LINQ provider (assuming it isn't LINQ to Objects), you may want to add individual tests for each member of searchString. The best way to do this is probably using PredicateBuilder
var sq = PredicateBuilder.New<dbType>();
foreach (var s in searchString)
sq = sq.Or(r => r.largeString.Contains(s));
q = q.Where(sq);
I need a searching process on linq like this. For example, I will make searching on Name column,and user enters "Ca?an" word to textbox. Question mark will e used for sprecial search character for this sitution.
It will search by Name column and, find Canan,Calan,Cazan etc.
I hope I can explain my problem correctly.
Can anyone give me an idea about this linq query. Thank in advance...
You can use this regular expression (if you are using C#) to check for the "Ca?an".
string d = "DDDDDDDDDDCasanDDDDDDDDDD";
Regex r = new Regex(#"Ca([a-zA-Z]{1})an");
string t = r.Match(d).Value;
Output will be:
"Casan"
You have all your colum stored in a database, then do something like:
List<Person> list = new List<Person>(); //Filled
var res = list.Select(x => r.Match(x.Name));
Output will be a IEnumerable with all the "Persons" who contains in the Name "Ca?an", being ? no matter which letter
You need to convert your search-syntax into an existing search-engine - I'd suggest Regex. So the steps will be:
Safely convert the entered search-string into Regex-pattern
Perform the search in Linq on name-property
Solution:
1: Safely convert search string by replacing '?' with Regex-version of wildchar:
var userInput = "Ca?an";
var regexPattern = Regex.Escape(userInput).Replace(#"\?", ".");
2: Perform search in Linq (assuming itemList implements IEnumerable):
var results = itemList.Where(item => Regex.IsMatch(item.Name, regexPattern));
Hope this helps!
Basically all I'm looking to do is something like the following:
string EntityFrameworkType = "Product";
string searchField = "ProductName";
string searchValue = "My Product";
using( var context = new entitycontext())
{
var result = (from x in context.EntityFrameworkType.Where(l=>l.searchField == searchValue) select x).FirstOrDefault();
}
of course this syntax won't work because context does not contain an entity called "EntityFrameworkType"...
Is it possible to do this another way??? What I'm looking to do in generalize my database duplicate check. In this example, I'm searching for any Product with the Name "My Product". But I'd like to be able to pass in these string for say, ProductCategory with ProductCategoryId = 1.... etc...
you can have a look here to get the idea of how it is done.
You'll need to learn about Expression
I have the following xml string:
<report>
<item id="4219" Co="6063" LastName="Doe" FirstName="John"/>
<item id="2571" Co="6063" LastName="Doe" FirstName="Jane"/>
</report>
How do I write a Linq to xml query that filters by name = "Jane" and writes it back to a xml string
I have the following code so far:
XDocument reportXmlDoc = XDocument.Parse(_report); //This is the string var assigned with data above
var filteredList = from x in fullReportXmlDoc.Descendants("item")
where x.Attribute("FirstName").Value == "Jane"
select x;
How do I convert filteredList back to a xml string?
You can project a new XML tree from your existing query:
var filteredList =
from x in fullReportXmlDoc.Descendants("item")
where x.Attribute("FirstName").Value == "Jane"
select new XElement("report", x).ToString();
Or maybe, in a more general case:
string result = new XElement("report",
from x in fullReportXmlDoc.Descendants("item")
where x.Attribute("FirstName").Value == "Jane"
select x).ToString();
Have you tried using SaveOptions.DisableFormatting to get the XElement's XML string? It would look something like this for your case:
XDocument reportXmlDoc = XDocument.Parse(_report); //This is the string var assigned with data above
var elementStrings = from x in fullReportXmlDoc.Descendants("item")
where x.Attribute("FirstName").Value == "Jane"
select x.ToString(SaveOptions.DisableFormatting);
need help
I have this enum which sets the PayClassNo to Direct and Indirect. I want to use this enum in my LinQ query.
Here's my scratch LinQ query:
var jDef = from jd in context.GetTable<RJVDefinition>()
select new PayrollJVDefinition
{
JVdefNo = jd.JVDefNo,
AccntCode = jd.AccntCode,
AccntDesc = jd.AccntDesc,
PayClass = enum.GetValue(jd.PayClassNo),
IsFixed = jd.IsFixed,
IsEmployee = jd.IsFixed,
IsAR = jd.IsAR,
CreatedByNo = jd.CreatedByNo,
CreatedDate = jd.CreatedDate,
ModifiedByNo = jd.ModifiedByNo,
ModifiedDate = jd.ModifiedDate
};
Need help because I'm not sure if this will work.
You could certainly do the translation in code, similar to your example (using Enum.Parse), but you don't need to. You can use the designer to set the object property type to an enumerated value. See this article for details.
You just need to parse the Enum just use something like
Enum.Parse(jb.PayClassNo, YourEnumType)