本文整理汇总了C#中System.Linq.Where方法的典型用法代码示例。如果您正苦于以下问题:C# System.Linq.Where方法的具体用法?C# System.Linq.Where怎么用?C# System.Linq.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Linq
的用法示例。
在下文中一共展示了System.Linq.Where方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: linq_defers_execution
public void linq_defers_execution()
{
var array = new[] {"a", "b", "a", "a"};
var filteringValue = "a";
var results1 = array.Where(x => x == filteringValue);
filteringValue = "b";
var results2 = array.Where(x => x == filteringValue);
results1.Count().Should().Be(1);
results2.Count().Should().Be(1);
}
示例2: SplitSupportedQuery
public void SplitSupportedQuery()
{
IQueryable<string> queryRoot = new[] { "query", "root" }.AsQueryable();
IQueryable<string> query = queryRoot.Where(s => true).Take(5);
IQueryable supportedQuery;
IQueryable unsupportedQuery;
supportedQuery = QueryComposer.Split(query, out unsupportedQuery);
MethodCallExpression mce = supportedQuery.Expression as MethodCallExpression;
Assert.IsNotNull(mce,
"The first supported expression should be a method call.");
Assert.AreEqual("Take", mce.Method.Name,
"The first supported expression should be a Take call.");
Assert.AreEqual(2, mce.Arguments.Count,
"The first supported expression should have 2 arguments.");
MethodCallExpression mce2 = mce.Arguments[0] as MethodCallExpression;
Assert.IsNotNull(mce2,
"The second supported expression should be a method call.");
Assert.AreEqual("Where", mce2.Method.Name,
"The second supported expression should be a Where call.");
Assert.AreEqual(2, mce2.Arguments.Count,
"The second supported expression should have 2 arguments.");
ConstantExpression ce = mce2.Arguments[0] as ConstantExpression;
Assert.IsNotNull(ce,
"The first argument in the second supported expression should be constant.");
Assert.IsNull(unsupportedQuery,
"The unsupported query should be null.");
}
示例3: correct_iquerable_should_be_returned
public void correct_iquerable_should_be_returned()
{
var source = new[]
{
new DummyDocument
{
Name = "First",
},
new DummyDocument
{
Name = "Middle",
},
new DummyDocument
{
Name = "Last",
}
}
.AsQueryable();
var sortedData = source.DynamicFiltering("name", "Last");
var expectedSortedData = source.Where(document => document.Name == "Last");
sortedData.SequenceEqual(expectedSortedData).Should().BeTrue();
}
示例4: Foo
public void Foo()
{
// Single accepts a predicate - the Where is superfluous
var stuff = new []{"Josh", "was", "here"};
var less = stuff.Where(x => x.StartsWith("J")).Single();
}
示例5: GetItems
/*begin*/
public ActionResult GetItems(GridParams g, string[] selectedColumns, bool? choosingColumns)
{
//when setting columns from here we don't get the grid defaults, so we have to specify Sortable, Groupable etc.
var columns = new[]
{
new Column { Name = "Id", Width = 70, Order = 1 },
new Column { Name = "Person", Sortable = true, Groupable = true, GroupRemovable = true, Order = 2 },
new Column { Name = "Food", Sortable = true, Groupable = true, GroupRemovable = true, Order = 3 },
new Column { Name = "Location", Sortable = true, Groupable = true, GroupRemovable = true, Order = 4 },
new Column { Name = "Date", Sortable = true, Groupable = true, GroupRemovable = true, Width = 100, Order = 5 },
new Column { Name = "Price", Sortable = true, Groupable = true, GroupRemovable = true, Width = 100, Order = 6 },
};
var baseColumns = new[] { "Id", "Person" };
//first load
if (g.Columns.Length == 0)
{
g.Columns = columns;
}
if (choosingColumns.HasValue && selectedColumns == null)
{
selectedColumns = new string[] { };
}
if (selectedColumns != null)
{
//make sure we always have Id and Person columns
selectedColumns = selectedColumns.Union(baseColumns).ToArray();
var currectColumns = g.Columns.ToList();
//remove unselected columns
currectColumns = currectColumns.Where(o => selectedColumns.Contains(o.Name)).ToList();
//add missing columns
var missingColumns = selectedColumns.Except(currectColumns.Select(o => o.Name)).ToArray();
currectColumns.AddRange(columns.Where(o => missingColumns.Contains(o.Name)));
g.Columns = currectColumns.ToArray();
}
var gridModel = new GridModelBuilder<Lunch>(Db.Lunches.AsQueryable(), g).Build();
// used to populate the checkboxlist
gridModel.Tag =
new
{
columns = columns.Select(o => o.Name).Except(baseColumns).ToArray(),
selectedColumns = g.Columns.Select(o => o.Name).Except(baseColumns).ToArray()
};
return Json(gridModel);
}
示例6: Load
public override void Load()
{
var types = new[] {typeof (ILogInAccounts), typeof (LogInAccounts), GetType()}
.Select(type => type.Assembly)
.SelectMany(assembly => assembly.GetTypes())
.ToArray();
var services = types.Where(t => t.IsInterface && !t.IsGenericTypeDefinition);
var implementations = types.Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericTypeDefinition);
services.SelectMany(t => implementations, (service, impl) => new {service, impl})
.Where(x => x.service.IsAssignableFrom(x.impl))
.GroupBy(x => x.service, x => x.impl)
.Where(g => g.Count() == 1)
.Select(g => new {service = g.Key, impl = g.Single()})
.ToList()
.ForEach(x => Kernel.Bind(x.service).To(x.impl).InRequestScope());
}
示例7: Complete
public override string[] Complete(Shell shell, string[] tokens)
{
var items = new[] {"users"};
if (tokens.Length == 0)
{
return items;
}
return tokens.Length == 1 ? items.Where(x => x.StartsWith(tokens[0])).ToArray() : null;
}
示例8: Switch
public void Switch()
{
var table = new[]
{
new { pre = ReelState.Stop, post = ReelState.Cycle },
new { pre = ReelState.Cycle, post = ReelState.Stop },
};
State = table.Where(t => t.pre == State)
.First()
.post;
}
示例9: LoadLanguageList
private void LoadLanguageList()
{
var settings = new[]
{
new DisplayLanguageSetting("en-US"),
new DisplayLanguageSetting("fr-CA"),
new DisplayLanguageSetting("sv-SV")
};
LanguageList.Items.AddRange(settings.Where(item => item.Exists).ToArray());
LanguageList.DisplayMember = "Name";
}
示例10: CanUsePredicates
public void CanUsePredicates()
{
var source = new[] { 1, 2, 3, 4, 5 };
var isEven = new Pred<int>(x => x%2 == 0);
var isOdd = isEven.Invert();
var isOddOr2 = isOdd.Or(x => x == 2);
var count = source.Where(isOddOr2).Count();
Assert.AreEqual(4, count);
}
示例11: Syoka
public void Syoka()
{
var table = new[]
{
new { pre = KakuhenMode.Normal, post = KakuhenMode.Kakuhen1 },
new { pre = KakuhenMode.Kakuhen1, post = KakuhenMode.Kakuhen2 },
new { pre = KakuhenMode.Kakuhen2, post = KakuhenMode.Kakuhen1 },
};
Mode = table.Where(t => t.pre == Mode)
.First()
.post;
}
示例12: ToString
public override string ToString()
{
var result = new[] {
VersionType.IsNullOrEmpty() ? "" : "\"_version_type\": " + VersionType.Quotate(),
Version == null ? "" : "\"_version\": " + Version.AsString(),
Routing.IsNullOrEmpty() ? "" : "\"_routing\": " + Routing.Quotate(),
Percolate.IsNullOrEmpty() ? "" : "\"_percolate\": " + Percolate.Quotate(),
Parent.IsNullOrEmpty() ? "" : "\"_parent\": " + Parent.Quotate(),
Timestamp.IsNullOrEmpty() ? "" : "\"_timestamp\": " + Timestamp.Quotate(),
Ttl.IsNullOrEmpty() ? "" : "\"_ttl\": " + Ttl.Quotate(),
};
return result.Where(s => !s.IsNullOrEmpty()).JoinWithComma();
}
示例13: Rebase
public void Rebase()
{
IQueryable<string> queryRoot = new[] { "query", "root" }.AsQueryable();
IQueryable<string> query = queryRoot.Where(s => true);
MethodCallExpression mce = query.Expression as MethodCallExpression;
Assert.IsNotNull(mce,
"Expression should be a method call.");
Assert.AreEqual("Where", mce.Method.Name,
"Expression should be a Where call.");
Assert.AreEqual(2, mce.Arguments.Count,
"Expression should have 2 arguments.");
ConstantExpression ce = mce.Arguments[0] as ConstantExpression;
Assert.IsNotNull(ce,
"The first expression argument should be constant.");
Assert.AreEqual(queryRoot, ce.Value,
"The first expression argument should be the query root.");
IQueryable<string> newQueryRoot = new[] { "new", "query", "root" }.AsQueryable();
IQueryable rebasedQuery = QueryComposer.Compose(newQueryRoot, query);
mce = rebasedQuery.Expression as MethodCallExpression;
Assert.IsNotNull(mce,
"Rebased expression should be a method call.");
Assert.AreEqual("Where", mce.Method.Name,
"Rebased expression should be a Where call.");
Assert.AreEqual(2, mce.Arguments.Count,
"Rebased expression should have 2 arguments.");
ce = mce.Arguments[0] as ConstantExpression;
Assert.IsNotNull(ce,
"The first rebased expression argument should be constant.");
Assert.AreEqual(newQueryRoot, ce.Value,
"The first rebased expression argument should be the new query root.");
}
示例14: LookLikeAValidGuildWarsTwoApiKey
/// <summary>
/// Checks that the API key passed looks like a GW2 API key (follows a length pattern between delimiters and is alpha numeric).
/// </summary>
/// <param name="possibleKey">
/// The possible API key.
/// </param>
/// <returns>
/// True if the possibleKey looks like a valid GW2 API key.
/// </returns>
public static bool LookLikeAValidGuildWarsTwoApiKey(string possibleKey)
{
const char Delimiter = '-';
var expectedTokenLengths = new [] { 8, 4, 4, 4, 20, 4, 4, 4, 12 };
var tokens = possibleKey.Split(Delimiter);
var actualTokenLengths = tokens.Select(t => t.Length).ToArray();
if (expectedTokenLengths.Length != actualTokenLengths.Length)
{
return false;
}
if (expectedTokenLengths.Where((t, index) => actualTokenLengths[index] != t).Any())
{
return false;
}
var allAlphaNumericChars = tokens.All(t => t.All(char.IsLetterOrDigit));
return allAlphaNumericChars;
}
示例15: PredictiveSearch
public ActionResult PredictiveSearch(string keywordStartsWith)
{
var data = new[] { "London", "Manchester", "Manchester Airport", "London Kings Cross", "London Euston" };
var results = data.Where(s => s.StartsWith(keywordStartsWith)).ToArray();
return new JsonResult { ContentEncoding = Encoding.UTF8, Data = results, ContentType = "application/json", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}