Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#查找最佳匹配元素/简化查询到列表_C#_Linq - Fatal编程技术网

C#查找最佳匹配元素/简化查询到列表

C#查找最佳匹配元素/简化查询到列表,c#,linq,C#,Linq,我问自己,怎样才能简化这样的事情 var myList = new List<MyObject> pulic MyObject FindBestMatching(int Prop1Value, int Prop2Value, int Prop3Value) { MyObject item = null; item = myList.Find(x => x.Prop1 == Prop1Value && x.Prop2 == Prop2Value &

我问自己,怎样才能简化这样的事情

var myList = new List<MyObject>
pulic MyObject FindBestMatching(int Prop1Value, int Prop2Value, int Prop3Value)
{
    MyObject item = null;
    item = myList.Find(x => x.Prop1 == Prop1Value && x.Prop2 == Prop2Value && x.Prop3 == Prop3Value);
    if(item != null)
    {
         return item;
    }
    item = myList.Find(x => x.Prop1 == Prop1Value && x.Prop2 == Prop2Value);
    if(item != null)
    {
         return item;
    }
    item = myList.Find(x => x.Prop1 == Prop1Value);
    // Doesn't matter if its null
    return item;
}
var myList=新列表
pulic MyObject FindBestMatching(int-Prop1Value、int-Prop2Value、int-Prop3Value)
{
MyObject项=null;
item=myList.Find(x=>x.Prop1==Prop1Value&&x.Prop2==Prop2Value&&x.Prop3==Prop3Value);
如果(项!=null)
{
退货项目;
}
item=myList.Find(x=>x.Prop1==Prop1Value&&x.Prop2==Prop2Value);
如果(项!=null)
{
退货项目;
}
item=myList.Find(x=>x.Prop1==Prop1Value);
//不管它是否为空
退货项目;
}
我肯定LINQ提供了一个解决方案,但我找不到:)

谢谢。

试试这个:

public MyObject FindBestMatching(int Prop1Value, int Prop2Value, int Prop3Value)
{
    return myList.FirstOrDefault(x => (x.Prop1 == Prop1Value && x.Prop2 == Prop2Value && x.Prop3 == Prop3Value)
    || (x.Prop1 == Prop1Value && x.Prop2 == Prop2Value)
    || (x => x.Prop1 == Prop1Value));
}

从技术上讲,您可以将当前代码简化为

  pulic MyObject FindBestMatching(int Prop1Value, int Prop2Value, int Prop3Value) {
    return 
         myList.Find(x => x.Prop1 == Prop1Value && x.Prop2 == Prop2Value && x.Prop3 == Prop3Value)
      ?? myList.Find(x => x.Prop1 == Prop1Value && x.Prop2 == Prop2Value)
      ?? myList.Find(x => x.Prop1 == Prop1Value);    
  }
但是执行
Find
(扫描整个列表)可能是一项代价高昂的操作,如果是您的情况,您只能在一个循环中找到最佳匹配:

  public MyObject FindBestMatching(int Prop1Value, int Prop2Value, int Prop3Value) {
    MyObject result1 = null;
    MyObject result2 = null;

    foreach (MyObject item in myList) {
      if (item.Prop1 == Prop1Value) {
        result1 = item;

        if (item.Prop2 == Prop2Value) {
          result2 = item;

          if (item.Prop3 == Prop3Value) 
            return item; 
        }
      }  
    }

    return result2 ?? result1; 
  }

为什么你认为有一个更容易的方法?您可以使用
FirstOrDefault
,但仍然需要空检查。我认为你的解决方案已经是最好的了,至少它是可读的和不可理解的,这才是真正重要的——至少对我来说是这样。这不起作用。它只找到第一个元素,其中一个条件为真谢谢你的回答。一个提示:我认为if(x.Prop1==Prop1Value)之后有一个{missing}@ErWu:你是对的,它应该是
{
之后的
if(item.Prop1==Prop1Value)
。很抱歉输入错误。我已经编辑了答案