简体   繁体   English

使用 C# Linq 从多级数据存储中查找并返回字符串值

[英]Using C# Linq find and return a string value from a multi-level data store

I have a custom config stored in memory DomainConfigElements .我有一个自定义配置存储在 memory DomainConfigElements中。

  <RemoteAppConfiguration>
    <domains>
      <domain name="abc" isenabled="true">
        <remoteapps>
          <remoteapp name="app1" applicationname="app1shortcut" isenabled="true" />
          <remoteapp name="app2" applicationname="app2shortcut" isenabled="true" />
        </remoteapps>
      </domain>
      <domain name="efg" isenabled="false">
        <remoteapps>
          <remoteapp name="app3" applicationname="app3exe" isenabled="true" />
          <remoteapp name="app4" applicationname="app4exe" isenabled="true" />
        </remoteapps>        
      </domain>
      <domain name="xyz" isenabled="true">
        <remoteapps>
          <remoteapp name="app5" applicationname="app5launcher" isenabled="true" />
          <remoteapp name="app6" applicationname="app6launcher" isenabled="true" />
        </remoteapps>        
      </domain>
    </domains>    
  </RemoteAppConfiguration>

I have to find an app app5 and return the applicationanme .我必须找到一个应用app5并返回applicationanme In this example, I have to return a string app5launcher .在这个例子中,我必须返回一个字符串app5launcher If I pass app2 then the code should return app2shortcut .如果我通过app2那么代码应该返回app2shortcut

I wanted to use linq to get the desired result.我想使用linq来获得所需的结果。 This is what I came with:这是我带来的:

var fixedParam = "app6";
var apps = AppUtilities.DomainConfigElements.Where(d => d.IsEnabled)
         .Select(d => d.RemoteApps
         .Where(r => string.Equals(r.Name, fixedParam, StringComparison.InvariantCultureIgnoreCase))).FirstOrDefault();
var appName = apps.Select(a => a.ApplicationName).FirstOrDefault();

It gets me the result, but looks complex.它让我得到了结果,但看起来很复杂。 I feel this can be simplified.我觉得这可以简化。 I wanted to know is there a better way of doing this?我想知道有没有更好的方法来做到这一点? I tried to ask this question in codereview.stackexchange but the website seems to be not working right now.我试图在codereview.stackexchange中提出这个问题,但该网站现在似乎无法正常工作。

Updated answer:更新的答案:

The original LINQ query you posted is picking a list of RemoteApp that match the criteria Name to fixedParam .您发布的原始 LINQ 查询正在选择符合条件NamefixedParamRemoteApp列表。 The final .FirstOrDefault() query in the first LINQ statement picks first item from that list and stores in apps .第一个 LINQ 语句中的最终.FirstOrDefault()查询从该列表中选择第一个项目并存储在apps中。

2nd LINQ query apps.Select(a => a.ApplicationName).FirstOrDefault();第二个 LINQ 查询apps.Select(a => a.ApplicationName).FirstOrDefault(); just picks ApplicationName attribute from apps , which should only be one item anyway.只需从apps中选择ApplicationName属性,无论如何它应该只是一项。 Then, FirstOrDefault() is called again.然后,再次调用FirstOrDefault()

The main reason this is inefficient is because of redundant FirstOrDefault() calls.效率低下的主要原因是多余的FirstOrDefault()调用。 However, it does handle case where multiple RemoteApp items have same Name attribute.但是,它确实可以处理多个RemoteApp项具有相同Name属性的情况。

My solution starts by picking enabled Domain object from DomainConfigElements .我的解决方案首先从DomainConfigElements选择启用的Domain object 。 There is an additional query in here that ensures the Domain being picked also contains the RemoteApp object with Name == FixedParam .这里有一个附加查询,可确保选择的Domain包含具有Name == FixedParamRemoteApp object。 Once the correct Domain is selected, I query on RemoteApps in the domain and pick RemoteApp that matches Name == fixedParam .选择正确的Domain后,我会查询域中的RemoteApps并选择与Name == fixedParam匹配的RemoteApp Then, we just get ApplicationName property.然后,我们只需获取ApplicationName属性。

AppUtilities.DomainConfigElements
                .Where(d => d.IsEnabled) // get enabled domains
                .Select(d => d.RemoteApps // query on remote apps inside domain
                    .FirstOrDefault(app => app.Name == "app2")) 
                .FirstOrDefault(d => d.Name == "app2").ApplicationName; // query on fixedParam

I mocked up your class structure in my own IDE to get the statements correct, but I have not run it against your XML yet.我在我自己的 IDE 中模拟了您的 class 结构以使语句正确,但我还没有针对您的 XML 运行它。 Let me know if this works for you, otherwise I will remove:)让我知道这是否适合您,否则我将删除:)

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

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