我已經嘗試了一整天,并進行了多次嘗試,但都沒有成功。我有以下的Linq查詢,回傳結果。
這個查詢回傳IEnumerable<sketchEntity>:
var temp = from entityin remainingEntities
where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
選擇物體。
這個查詢回傳(IEnumerable<sketchEntity>, end)
var temp = (from entityin remainingEntities
where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
select entity, ChainMatchType.End)。)
這兩個都不是我想要的。最終的IEnumerable中的每個結果都需要是一個命名的元組,每個元組都有一個ISketchEntity和ChainMatchType:
List<(ISketchEntity sketchEntityName, ChainMatchType MatchName) >
我將在三個不同的時間進行類似的查詢。
- 當某些型別的ISketchEntities與EndPoint匹配時。
- 當某些型別的 ISketchEntities 匹配一個 StartPoint 時。
- 當某些型別的 ISketchEntities 匹配一個 CenterPoint 時。
當我運行每個查詢時,我想使用Enum添加結果的型別來表示匹配的型別。
public enum ChainMatchType
{
開始。
結束。
中心
}
我的計劃是在回傳結果之前將所有三個查詢合并為一個結果。
我怎樣才能將我的LINQ格式化,以便將結果變成一個命名的元組:
Name DataType
物體。 ISketchEntity
匹配型別。 ChainMatchType
EDIT FuzzyEquals是一個自定義擴展。它使用 /-公差來比較兩個點。我正在處理CAD資料,過去的歷史告訴我,有時兩個點可以接近到相等,但并沒有完全相同的坐標。
public static bool FuzzyEqual(thisISketchPoint sp, ISketchPoint other, double tolerance)。
{
if (
sp.X > other.X - tolerance && sp.X < other.X tolerance &&
sp.Y > other.Y - tolerance && sp.Y < other.Y tolerance &&
sp.Z > other.Z - tolerance && sp.Z < other.Z tolerance
)
return true。
return false;
剛才看了一下,我想可以簡化為:
public static bool FuzzyEqual(thisISketchPoint sp, ISketchPoint other, double tolerance)。
{
return sp.X > other.X - tolerance && sp.X < other.X tolerance & &
sp.Y > other.Y - tolerance && sp.Y < other.Y tolerance &&
sp.Z > other.Z - tolerance && sp.Z < other.Z tolerance。
}
uj5u.com熱心網友回復:
- 我建議使用只有擴展方法的Linq,并避免使用關鍵字式的
from,in,where,select。- 我承認這是我的個人偏好--但是C#的Linq關鍵字功能有些不自然,而且你幾乎總是最終需要使用一些擴展方法(尤其是
ToList和Include),而混合關鍵字和擴展方法的風格使得事情更難閱讀。
- 我承認這是我的個人偏好--但是C#的Linq關鍵字功能有些不自然,而且你幾乎總是最終需要使用一些擴展方法(尤其是
- 無論如何,你只需要在
Select步驟中添加值元組,它可以是一個方法引數(盡管你不能easily將值元組成員名稱引數化--這是可能的,但超出了這個問題的范圍)。
就像這樣:
List<( ISketchEntity sketchEntityName, ChainMatchType matchName ) > withName = remainingEntities
.Where( e => e.EndPoint.FuzzyEqual( matchPoint, _tolerance ) )
.Select( e => ( sketchEntityName: e, matchName: matchName )
.ToList()
作為一個引數化的方法:
(我不知道matchPoint和_tolerance是什么)。
public static List< ( ISketchEntity sketchEntityName, ChainMatchType matchName )> Get2( this IEnumerable< ISketchEntity> entities, ChainMatchType matchType )
{
//預設條件:
if( entities is null ) throw new ArgumentNullException(nameof(entities)) 。
return entities
.Where( e => e.EndPoint.FuzzyEqual( matchPoint, _tolerance ) )
.Select( e => ( sketchEntityName: e, matchName: matchType )
.ToList()
}
uj5u.com熱心網友回復:
你只需要在選擇部分周圍加上大括號:
var temp = (from entities in remainingEntities
where entity.EndPoint.FuzzyEqual(matchPoint, _tolerance)
select(物體,ChainMatchType.End))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/327604.html
標籤:
上一篇:回圈瀏覽一個IEnumerable,并將它們傳遞給一個帶有幾個if/else條件的模型
下一篇:通過活動傳遞投入
