C# linq如何构建where谓词
我想查询一个IEnumerable的形状。我有不同类型的形状,它们与给定的坐标有不同的关系,这取决于形状的类型 对于任何给定的坐标,我想找到它的相关形状。我想用Linq来做这个。但是由于缺乏理解而失去了平衡。我已经搜索和阅读了几个小时,但是我能找到正确的单词来给我一个我正在尝试做的例子。下面是一些代码,希望能显示我想做的事情的概念,但显然不起作用。必须有一种链接这些表达式的方法-我见过谓词生成器,可能会用到它,但我想先了解更多的基础知识。 我该怎么做C# linq如何构建where谓词,c#,linq,linq-expressions,C#,Linq,Linq Expressions,我想查询一个IEnumerable的形状。我有不同类型的形状,它们与给定的坐标有不同的关系,这取决于形状的类型 对于任何给定的坐标,我想找到它的相关形状。我想用Linq来做这个。但是由于缺乏理解而失去了平衡。我已经搜索和阅读了几个小时,但是我能找到正确的单词来给我一个我正在尝试做的例子。下面是一些代码,希望能显示我想做的事情的概念,但显然不起作用。必须有一种链接这些表达式的方法-我见过谓词生成器,可能会用到它,但我想先了解更多的基础知识。 我该怎么做 using System; using Sy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace LinqLearning
{
public class Coordinate
{
public double X { get; set; }
public double Y { get; set; }
}
public abstract class Bounded
{
public Coordinate TopRight { get; set; }
public Coordinate BottomLeft { get; set; }
}
public class Shape1 : Bounded
{ }
public class Shape2 : Bounded
{ }
public class LinqExperiments
{
public IEnumerable<Bounded> GetSquaresNearPoint(IEnumerable<Bounded> shapesEnumerable, Coordinate locator)
{
Expression<Func<Bounded, Coordinate, bool>> Shape1NearCoordinate =
(shape, coord) => shape.TopRight.Y > coord.Y &&
shape.BottomLeft.Y < coord.Y &&
shape.TopRight.X == coord.X;
Expression<Func<Bounded, Coordinate, bool>> Shape2NearCoordinate =
(shape, coord) => shape.TopRight.Y == coord.Y &&
shape.TopRight.X < coord.X + 3 &&
shape.TopRight.X > coord.X - 3;
Expression<Func<IQueryable<Bounded>, Coordinate, bool>> predicate = (shapes, coord) => Shape1NearCoordinate || Shape2NearCoordinate;
return shapesEnumerable.AsQueryable().Where(predicate);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Linq.Expressions;
名称空间LinqLearning
{
公共类坐标
{
公共双X{get;set;}
公共双Y{get;set;}
}
公共抽象类有界
{
公共坐标TopRight{get;set;}
公共坐标左下角{get;set;}
}
公共类形状1:有界
{ }
公共类Shape2:有界
{ }
公共类LINQ实验
{
公共IEnumerable GetSquaresNearPoint(IEnumerable shapesEnumerable,坐标定位器)
{
表达式形状1近坐标=
(shape,coord)=>shape.TopRight.Y>coord.Y&&
形状.左下角.Y<坐标.Y&&
shape.TopRight.X==coord.X;
表达式形状2近坐标=
(shape,coord)=>shape.TopRight.Y==coord.Y&&
形状.TopRight.Xcoord.X-3;
表达式谓词=(形状,坐标)=>Shape1NearCoordinate | | Shape2NearCoordinate;
返回shapesEnumerable.AsQueryable().Where(谓词);
}
}
}
我真的不明白为什么要使用表达式和IQueryable
s。我认为这可以通过Func
解决:
public IEnumerable GetSquaresNearPoint(IEnumerable shapesEnumerable,坐标定位器)
{
Func形近坐标=
(shape,coord)=>shape.TopRight.Y>coord.Y&&
形状.左下角.Y<坐标.Y&&
shape.TopRight.X==coord.X;
Func形近坐标=
(shape,coord)=>shape.TopRight.Y==coord.Y&&
形状.TopRight.Xcoord.X-3;
Func谓词=(形状,坐标)=>shape1nearcoord(形状,坐标)| | shape2nearcoord(形状,坐标);
返回shapenumerable.Where(x=>谓词(x,定位器));
}
我真的不明白为什么要使用表达式和IQueryable
s。我认为这可以通过Func
解决:
public IEnumerable GetSquaresNearPoint(IEnumerable shapesEnumerable,坐标定位器)
{
Func形近坐标=
(shape,coord)=>shape.TopRight.Y>coord.Y&&
形状.左下角.Y<坐标.Y&&
shape.TopRight.X==coord.X;
Func形近坐标=
(shape,coord)=>shape.TopRight.Y==coord.Y&&
形状.TopRight.Xcoord.X-3;
Func谓词=(形状,坐标)=>shape1nearcoord(形状,坐标)| | shape2nearcoord(形状,坐标);
返回shapenumerable.Where(x=>谓词(x,定位器));
}
为什么不坚持简单的函数呢?当实际不需要时,无需使用Lambda
或Linq表达式
static bool Shape1NearCoordinate(Bounded shape, Coordinate coord) {
return shape.TopRight.Y > coord.Y &&
shape.BottomLeft.Y < coord.Y &&
shape.TopRight.X == coord.X;
}
static bool Shape2NearCoordinate(Bounded shape, Coordinate coord) {
return shape.TopRight.Y == coord.Y &&
shape.TopRight.X < coord.X + 3 &&
shape.TopRight.X > coord.X - 3;
}
public IEnumerable<Bounded> GetSquaresNearPoint(IEnumerable<Bounded> shapesEnumerable, Coordinate locator) {
return shapesEnumerable.Where(shape => Shape1NearCoordinate(shape, locator) || Shape2NearCoordinate(shape, locator));
}
静态布尔形状1近坐标(有界形状、坐标坐标坐标){
返回形状。右上角.Y>坐标.Y&&
形状.左下角.Y<坐标.Y&&
shape.TopRight.X==coord.X;
}
静态布尔形状2近坐标(有界形状、坐标坐标坐标){
返回shape.TopRight.Y==coord.Y&&
形状.TopRight.Xcoord.X-3;
}
公共IEnumerable GetSquaresNearPoint(IEnumerable shapesEnumerable,坐标定位器){
返回shapenumerable.Where(shape=>Shape1NearCoordinate(shape,locator)| | Shape2NearCoordinate(shape,locator));
}
为什么不坚持简单的函数呢?当实际不需要时,无需使用Lambda
或Linq表达式
static bool Shape1NearCoordinate(Bounded shape, Coordinate coord) {
return shape.TopRight.Y > coord.Y &&
shape.BottomLeft.Y < coord.Y &&
shape.TopRight.X == coord.X;
}
static bool Shape2NearCoordinate(Bounded shape, Coordinate coord) {
return shape.TopRight.Y == coord.Y &&
shape.TopRight.X < coord.X + 3 &&
shape.TopRight.X > coord.X - 3;
}
public IEnumerable<Bounded> GetSquaresNearPoint(IEnumerable<Bounded> shapesEnumerable, Coordinate locator) {
return shapesEnumerable.Where(shape => Shape1NearCoordinate(shape, locator) || Shape2NearCoordinate(shape, locator));
}
静态布尔形状1近坐标(有界形状、坐标坐标坐标){
返回形状。右上角.Y>坐标.Y&&
形状.左下角.Y<坐标.Y&&
shape.TopRight.X==coord.X;
}
静态布尔形状2近坐标(有界形状、坐标坐标坐标){
返回shape.TopRight.Y==coord.Y&&
形状.TopRight.Xcoord.X-3;
}
公共IEnumerable GetSquaresNearPoint(IEnumerable shapesEnumerable,坐标定位器){
返回shapenumerable.Where(shape=>Shape1NearCoordinate(shape,locator)| | Shape2NearCoordinate(shape,locator));
}
看起来您正在尝试或
两个复合条件,即Shape1NearCoordinate
和Shape2NearCoordinate
。您可以在不使用任何LINQ表达式的情况下执行此操作。你只是用它们来学习,还是我没有遵循一些更好的观点?是的,主要是为了学习。但是我有一个用例,如果这些表达式可以重用,那就太好了。通常,我希望每个形状在坐标处各有一个。我想在一个查询中从形状列表中获取两个形状。它们可以是Func
本身