简体   繁体   English

我可以在LINQ查询中使用正则表达式或调用方法吗?

[英]Can I use regular expressions or call a method inside a LINQ query?

I have this LINQ query: 我有这个LINQ查询:

var webWordForms = 
   .Select(def => new WebWordForm
   {
       definition = def.definition,
       partOfSpeech = definition.partOfSpeech,
       sourceId = 1,
       synonyms = def.synonyms
   })
   .ToList();

Here's the WebWordForm class: 这是WebWordForm类:

public class WebWordForm
{
    public string definition { get; set; }
    public string partOfSpeech { get; set; }
    public int sourceId { get; set; }
    public List<string> examples { get; set; }
}

What I need to do is to parse the data that's contained in def.definition part of the query and put this into two properties: definition and examples. 我需要做的是解析查询的def.definition部分中包含的数据,并将其放入两个属性中:定义和示例。

Here's an example of the typical data in def.definition. 这是def.definition中典型数据的示例。 There's an empty line in between each example. 每个示例之间都有一个空行。

 the trait of lacking restraint or control; freedom from inhibition or worry; "she danced with abandon"

 a feeling of extreme emotional intensity; "the wildness of his anger"

 forsake, leave behind; "We abandoned the old car in the empty parking lot"

 stop maintaining or insisting on; of ideas, claims, etc.; "He abandoned the thought of asking for her hand in marriage"; "Both sides have to give up some calims in these negociations"

 give up with the intent of never claiming again; "Abandon your life to God"; "She gave up her children to her ex

 leave behind empty; move out of; "You must vacate your office by tonight"

 leave someone who needs or counts on you; leave in the lurch; "The mother deserted her children"

Here is an explanation of the data: 这是数据的说明:

  1. The first part of the data up until the ; " 数据的第一部分直到; " ; " is the definition. ; "是的定义。
  2. The remainder of the data after the first ; " 剩下的数据在第一个之后; " ; " is one or more of List examples ; "是列表示例中的一个或多个

What I need is: 我需要的是:

  1. For the definition to be converted so the first character is a capital letter and for it to go into: public string definition { get; set; } 为了将定义转换为第一个字符为大写字母,并且将其输入: public string definition { get; set; } public string definition { get; set; } public string definition { get; set; } #1 public string definition { get; set; } #1

  2. For the examples after this to go into: public List<string> examples { get; set; } 对于下面的示例,请参见: public List<string> examples { get; set; } public List<string> examples { get; set; } public List<string> examples { get; set; } #2 public List<string> examples { get; set; } #2

Example

var webWordForms = 
   .Select(def => new WebWordForm
   {
       definition = #1
       examples = #2
       partOfSpeech = definition.partOfSpeech,
       sourceId = 1,
       synonyms = def.synonyms
   })
   .ToList();

I know this is not a simple question but I would appreciate any advice and suggestions on how I can do this 我知道这不是一个简单的问题,但是对于我如何做到这一点我将不胜感激

It is possible, you could do this. 有可能,您可以这样做。

var webWordForms = webforms  // this was just added, use actual collection.
   .Select(def => 
    { 
       string[] splits = def.definition.Split(new string[] {@"; """}, StringSplitOptions.RemoveEmptyEntries);
       return new WebWordForm
       {

           definition = splits[0],
           examples =  splits.Skip(1).ToList(),
           partOfSpeech = definition.partOfSpeech,
           sourceId = 1,
           synonyms = def.synonyms
       }
    })
   .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM