本文整理汇总了C#中Collection.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# Collection.FirstOrDefault方法的具体用法?C# Collection.FirstOrDefault怎么用?C# Collection.FirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Collection
的用法示例。
在下文中一共展示了Collection.FirstOrDefault方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FirstOrFirstDefault
public void FirstOrFirstDefault()
{
ICollection<object> strings = new Collection<object>
{
"a",
"b",
"c"
};
Assert.AreEqual("a", strings.FirstOrDefault(x => x.ToString().Equals("a", StringComparison.OrdinalIgnoreCase)));
Assert.IsNull(strings.FirstOrDefault(x => x == null));
}
示例2: SummateProducts
private static Collection<ProductDetail> SummateProducts(IEnumerable<ProductDetail> productCollection)
{
//Create a new collection of products.
Collection<ProductDetail> collection = new Collection<ProductDetail>();
//Iterate through the supplied product collection.
foreach (ProductDetail product in productCollection)
{
//Create a product
ProductDetail productInCollection = null;
if (collection.Count > 0)
{
productInCollection = collection.FirstOrDefault(x => x.ItemCode == product.ItemCode && x.ItemName == product.ItemName && x.Unit == product.Unit && x.Price == product.Price && x.TaxCode == product.TaxCode);
}
if (productInCollection == null)
{
collection.Add(product);
}
else
{
productInCollection.Quantity += product.Quantity;
productInCollection.Amount += product.Amount;
productInCollection.Discount += product.Discount;
productInCollection.Subtotal += product.Subtotal;
productInCollection.ShippingCharge += product.ShippingCharge;
productInCollection.Tax += product.Tax;
productInCollection.Total += product.Total;
}
}
return collection;
}
示例3: getField
private static FieldInfo getField(string key, Collection<FieldInfo> fields)
{
if (fields != null)
{
FieldInfo field = fields.FirstOrDefault<FieldInfo>(f => f.Name == key);
if (field != null)
return field;
}
return null;
}
示例4: GetTabResults
public ITabResult GetTabResults(ITabQuery query)
{
IEnumerable<ITabResult> results = new Collection<ITabResult>();
foreach(ITabHandler handler in this.tabHandlers){
IEnumerable<ITabResult> handlerMatches = handler.GetTabResults(this.client, query);
if (handlerMatches != null && handlerMatches.Count() > 0)
{
results = results.Concat(handlerMatches);
}
}
return results.FirstOrDefault();
}
示例5: ScanSpecialNsAndSetIfNoPreviousValue
private void ScanSpecialNsAndSetIfNoPreviousValue(Collection<AttributeAssignment> attributeAssignments)
{
if (specialPrefix != null)
{
return;
}
var attributeAssignment = attributeAssignments.FirstOrDefault(IsNsThatMatchesSpecialValue);
if (attributeAssignment!=null)
{
specialPrefix = attributeAssignment.Locator.PropertyName;
}
}
示例6: FindPath
public static List<Point> FindPath(int[,] field, Point start, Point goal)
{
//step 1
var closedSet = new Collection<PathNode>();
var openSet = new Collection<PathNode>();
//step 2
PathNode startNode = new PathNode()
{
Position = start,
CameFrom = null,
PathLengthFromStart = 0,
HeuristicEstimatePathLength = GetHeuristicPathLength(start, goal)
};
openSet.Add(startNode);
while (openSet.Count > 0)
{
//step 3
var currentNode = openSet.OrderBy(node =>
node.EstimateFullPathLength).First();
//step 4.
if (currentNode.Position == goal)
return GetPathForNode(currentNode);
//step 5.
openSet.Remove(currentNode);
closedSet.Add(currentNode);
//step 6.
foreach (var neighbourNode in GetNeighbours(currentNode, goal, field))
{
//step 7.
if (closedSet.Count(node => node.Position == neighbourNode.Position) > 0)
continue;
var openNode = openSet.FirstOrDefault(node =>
node.Position == neighbourNode.Position);
//step 8.
if (openNode == null)
openSet.Add(neighbourNode);
else
if (openNode.PathLengthFromStart > neighbourNode.PathLengthFromStart)
{
//step 9.
openNode.CameFrom = currentNode;
openNode.PathLengthFromStart = neighbourNode.PathLengthFromStart;
}
}
}
//step 10.
return null;
}
示例7: FindPath
public List<Cell> FindPath(GameField field, Cell start, Cell goal, Func<Cell, Cell, float> weightFunc = null)
{
var closedSet = new Collection<PathNode>();
var openSet = new Collection<PathNode>();
PathNode startNode = new PathNode()
{
Position = start,
CameFrom = null,
PathLengthFromStart = 0,
HeuristicEstimatePathLength = GetHeuristicPathLength(start, goal)
};
openSet.Add(startNode);
while (openSet.Count > 0)
{
var currentNode = openSet.OrderBy(node =>
node.EstimateFullPathLength).First();
if (currentNode.Position == goal)
return GetPathForNode(currentNode);
openSet.Remove(currentNode);
closedSet.Add(currentNode);
foreach (var neighbourNode in GetNeighbours(currentNode, goal, field, weightFunc))
{
if (closedSet.Count(node => node.Position == neighbourNode.Position) > 0)
continue;
var openNode = openSet.FirstOrDefault(node =>
node.Position == neighbourNode.Position);
if (openNode == null)
openSet.Add(neighbourNode);
else
if (openNode.PathLengthFromStart > neighbourNode.PathLengthFromStart)
{
openNode.CameFrom = currentNode;
openNode.PathLengthFromStart = neighbourNode.PathLengthFromStart;
}
}
}
return null;
}
示例8: UnionCallsAndFailedCallsData
private Collection<CallsData> UnionCallsAndFailedCallsData(
Collection<PerfCounterData> totalCallsPerfCounterDataCollection,
Collection<PerfCounterData> failedCallsPerfCounterDataCollection)
{
Collection<CallsData> callsAndFailedCallsDataCollection =
new Collection<CallsData>();
foreach (PerfCounterData totalCallsPerfCounterData in totalCallsPerfCounterDataCollection)
{
PerfCounterData matchingFailedCallsPerfCounterData =
failedCallsPerfCounterDataCollection.FirstOrDefault(a => a.CounterName ==
totalCallsPerfCounterData.CounterName);
CallsData callsData = new CallsData();
callsData.CounterName = totalCallsPerfCounterData.CounterName;
callsData.FriendlyName = totalCallsPerfCounterData.FriendlyName;
callsData.TotalCalls = totalCallsPerfCounterData.MaxValue;
callsData.TotalFailedCalls = matchingFailedCallsPerfCounterData.MaxValue;
callsData.PercentFailure = Math.Round(((Convert.ToDouble(callsData.TotalFailedCalls) /
Convert.ToDouble(callsData.TotalCalls)) * 100), 2);
callsAndFailedCallsDataCollection.Add(callsData);
}
return callsAndFailedCallsDataCollection;
}
示例9: GenerateFilesButtonBase_OnClick
public void GenerateFilesButtonBase_OnClick(object sender, RoutedEventArgs routedEventArgs)
{
if(string.IsNullOrEmpty(m_filePath))
{
return;
}
if (!Directory.Exists(m_filePath))
{
MessageBox.Show(string.Format("Path Does not exsist{0}", m_filePath),"Error",MessageBoxButton.OK,MessageBoxImage.Error);
}
Collection<Lag> lagCollection = new Collection<Lag>();
foreach (var lag in LagKilde)
{
var nyttLag = new Lag(lag);
lagCollection.Add(nyttLag);
}
foreach (var skive in Skiver)
{
var lagFunnet = lagCollection.FirstOrDefault(x => x.LagNummer == skive.LagNummer);
var funnetSkive = lagFunnet.SkiverILaget.FirstOrDefault(x => x.SkiveNummer == skive.SkiveNummer);
if (funnetSkive.Skytter != null)
{
funnetSkive.Skytter = new Skytter(funnetSkive.Skytter);
}
funnetSkive.Free = funnetSkive.Free;
}
m_databaseService.StoreToDatabase(lagCollection, this.m_filePath);
}
示例10: EnsureAxes
public override void EnsureAxes(Collection<IAxis> axes, IAxis defaultXAxis, IAxis defaultYAxis)
{
if (this.XAxisKey != null)
{
this.XAxis = axes.FirstOrDefault(a => a.Key == this.XAxisKey);
}
if (this.YAxisKey != null)
{
this.YAxis = axes.FirstOrDefault(a => a.Key == this.YAxisKey);
}
// If axes are not found, use the default axes
if (this.XAxis == null)
{
this.XAxis = defaultXAxis;
}
if (this.YAxis == null)
{
this.YAxis = defaultYAxis;
}
}
示例11: InitializeTypefaceList
/// <summary>
/// Gets the typefaces available for the selected font.
/// </summary>
/// <remarks>BoldSimulated and ObliqueSimulated font styles are filtered out of this collection. ArcGIS Pro does not render these styles.</remarks>
private void InitializeTypefaceList()
{
FontFamily family = new FontFamily();
if (SelectedFontFamily != null)
family = SelectedFontFamily.Font;
if (family != null)
{
ICollection<Typeface> faceCollection = family.GetTypefaces(); //family is a FontFamily object
List<TypefaceListItem> items = new List<TypefaceListItem>();
foreach (Typeface face in faceCollection)
{
if ((face.IsBoldSimulated) || (face.IsObliqueSimulated))
continue;
items.Add(new TypefaceListItem(face));
}
items.Sort();
TypeFaceCollection = new Collection<TypefaceListItem>(items);
SelectedTypeFace = TypeFaceCollection.FirstOrDefault();
}
}
示例12: GetTableIndexes
private static IEnumerable<DataIndexDbDefinition> GetTableIndexes(ITableDefinition table, string prefix)
{
const char EscapeCharacter = '!';
using (var ctx = GetRuntimeDatabaseConnectionManager())
{
var commandText = string.Format(CultureInfo.InvariantCulture, @"
SELECT
ind.name as IndexName
,t.name as ProcessName
,ind.filter_definition as FilterDefinition
,col.name as FieldName
,ic.is_included_column as IsIncluded
FROM
sys.indexes ind
INNER JOIN sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN sys.tables t ON ind.object_id = t.object_id AND ind.name LIKE '{0}%' ESCAPE '{1}'
WHERE ind.object_id = (SELECT OBJECT_ID(@tableName))
ORDER BY ind.name, ic.key_ordinal", AdoHelper.EscapeLikePattern(prefix, EscapeCharacter), EscapeCharacter);
using (var command = new SqlCommand(commandText, ctx.Connection))
{
command.Parameters.AddWithValue("@tableName", table.Name);
using (var reader = new SafeDataReader(command.ExecuteReader()))
{
var indexList = new Collection<DataIndexDbDefinition>();
while (reader.Read())
{
var indexName = reader.GetString(0);
var processName = reader.GetString(1);
var index = indexList.FirstOrDefault(ix => ix.IndexName == indexName && ix.ProcessName == processName);
if (index == null)
{
index = new DataIndexDbDefinition { IndexName = indexName, ProcessName = processName, FilterDefinition = reader.GetString(2) };
indexList.Add(index);
}
var fieldName = reader.GetString(3);
var included = reader.GetBoolean(4);
index.IndexFields.Add(new DataIndexFieldDbDefinition(fieldName, !included));
}
return indexList;
}
}
}
}
示例13: ParseOffer
private static void ParseOffer(Collection<Mapping.Offer> offers, Product product)
{
var offer = offers.FirstOrDefault();
if (offer != null)
{
// quantity
product.Stock = offer.Quantity;
// offerlistingid
product.Listing = offer.ListingID;
var price = offer.BuyingPrice;
if (price != null)
{
// price
product.Price = price.Value;
}
price = offer.PreviouslyPublishedPrice;
if (price != null)
{
// price
product.PreviousPrice = price.Value;
}
}
}
示例14: NewMethod
//.........这里部分代码省略.........
if (track != null && track.IsFull)
{
track = null;
}
if (track == null)
{
track = new Track();
allTracks.Add(track);
}
Talk currentTalk = queue.Dequeue();
if (!track.MorningSession.IsFull())
{
if (track.MorningSession.Talks.Count == 0)
{
currentTalk.Start = track.MorningSession.Start;
currentTalk.End = currentTalk.Start + currentTalk.Duration;
track.MorningSession.Talks.Add(currentTalk);
}
else
{
Talk lastTalkInTrack = track.MorningSession.Talks.Last();
currentTalk.Start = lastTalkInTrack.End;
currentTalk.End = currentTalk.Start + currentTalk.Duration;
if (track.MorningSession.IsValid(currentTalk))
{
track.MorningSession.Talks.Add(currentTalk);
}
else
{
if (mightNeedToBeRemovedTalk == null)
{
mightNeedToBeRemovedTalk = currentTalk;
currentTalk.Start = 0;
currentTalk.End = 0;
queue.Enqueue(currentTalk);
continue;
}
if (mightNeedToBeRemovedTalk == currentTalk)
{
Talk lastTalk = track.MorningSession.Talks.Last();
lastTalk.Start = 0;
lastTalk.End = 0;
track.MorningSession.Talks.RemoveAt(track.MorningSession.Talks.Count - 1);
queue.Enqueue(lastTalk);
}
currentTalk.Start = 0;
currentTalk.End = 0;
queue.Enqueue(currentTalk);
}
}
}
else
{
if (track.AfternoonSession.Talks.Count == 0)
{
currentTalk.Start = track.AfternoonSession.Start;
currentTalk.End = currentTalk.Start + currentTalk.Duration;
track.AfternoonSession.Talks.Add(currentTalk);
}
else
{
Talk lastTalkInTrack = track.AfternoonSession.Talks.Last();
currentTalk.Start = lastTalkInTrack.End;
currentTalk.End = currentTalk.Start + currentTalk.Duration;
if (track.AfternoonSession.IsValid(currentTalk))
{
track.AfternoonSession.Talks.Add(currentTalk);
}
else
{
currentTalk.Start = 0;
currentTalk.End = 0;
queue.Enqueue(currentTalk);
}
}
}
}
foreach (var item in allTalks)
{
if (item.IsLightning)
queue.Enqueue(item);
}
while (queue.Count > 0)
{
Talk currentTalk = queue.Dequeue();
var currentTrack = allTracks.FirstOrDefault(t => !t.IsFull);
currentTalk.Start = currentTrack.AfternoonSession.Talks.Last().End;
currentTrack.AfternoonSession.Talks.Add(currentTalk);
currentTrack.NetworkEvent.Start = 17;
}
return allTracks;
}
示例15: GetDistTile
public static Tile GetDistTile(int X, int Y, int Z)
{
try
{
Collection<Tile> tiles = new Collection<Tile>();
tiles = TilesFromScreen();
return tiles.FirstOrDefault(t => t.LocationOnMap == new exTibia.Objects.Location(X, Y, Z));
}
catch (InvalidOperationException ex)
{
Helpers.Debug.Report(ex);
}
return null;
}