Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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# LINQ忽略其中的someobject为null_C# - Fatal编程技术网

C# LINQ忽略其中的someobject为null

C# LINQ忽略其中的someobject为null,c#,C#,考虑以下函数 public static string UpdateCommand<T>(List<string> Except=null,List<string> Only=null) { if (Except != null && Only != null) { throw new Exception(); }

考虑以下函数

 public static string UpdateCommand<T>(List<string> Except=null,List<string> Only=null)
        {
           if (Except != null && Only != null)
              {
                 throw new Exception();
              }

        List<PropertyInfo> properties = typeof(T).GetProperties().ToList();


        if (Except != null)
        {
            for (int i = properties.Count; i-- > 0; )
            {
                if (Except.Contains(properties[i].Name)) properties.RemoveAt(i);
            }
        }

        if(Only != null)
        {
            for (int i = properties.Count; i-- > 0; )
            {
                if (!Only.Contains(properties[i].Name)) properties.RemoveAt(i);
            }
        }
        //etc...
        }
publicstaticstringupdateCommand(List-Except=null,List-Only=null)
{
if(除了!=null&&Only!=null)
{
抛出新异常();
}
List properties=typeof(T).GetProperties().ToList();
如果(除了!=null)
{
对于(int i=properties.Count;i-->0;)
{
if(除了.Contains(properties[i].Name))properties.RemoveAt(i);
}
}
如果(仅限!=null)
{
对于(int i=properties.Count;i-->0;)
{
如果(!Only.Contains(properties[i].Name))properties.RemoveAt(i);
}
}
//等等。。。
}
它需要两个可选参数,它们可以都为null,或者其中任何一个都可以有值,但至少其中一个应该为null

我试图找出上面的Linq语法,如果要比较的列表为空,是否有一种方法可以编写where语句,但忽略where语句

基本上,我正在寻找一种只使用LINQ编写上述内容的方法


我不能使用Intersect或Except,因为它介于两种不同类型之间

我建议您根本不要使用该方法管理筛选。相反,您可以这样做:

public static string UpdateCommand<T>(Func<IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> filterFunc = null)
{
    IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();

    if (filterFunc != null)
        properties = filterFunc(properties);

    ...
}
UpdateCommand(pis => pis.Where(pi => ...))

我建议你根本不用那种方法来管理过滤。相反,您可以这样做:

public static string UpdateCommand<T>(Func<IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> filterFunc = null)
{
    IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();

    if (filterFunc != null)
        properties = filterFunc(properties);

    ...
}
UpdateCommand(pis => pis.Where(pi => ...))
var结果=属性
.Where(p=>!(除了??Enumerable.Empty()).Any(s=>s==p.Name))
.Where(p=>Only==null | | Only.Any(s=>s==p.Name))
.ToArray();
var结果=属性
.Where(p=>!(除了??Enumerable.Empty()).Any(s=>s==p.Name))
.Where(p=>Only==null | | Only.Any(s=>s==p.Name))
.ToArray();

最近在一篇关于C#6.0可能的特性(第7项)的文章中讨论了这类问题。如果没有一些解决办法,例如前面的答案中所示,目前还不太可能实现。

最近在一篇关于可能的C#6.0功能(第7项)的文章中讨论了这类问题。如果没有一些解决办法,例如,如前一个答案所示,这在目前是不可能的