我的DataGridView.DataSource有问题,它耗费了我很多时间来解决这个问题.以下是代码:
string[] queryWords = singleQuery.Split(' '); // Split the query words according the "space" character
// Compose the SQL select query
string selectClause = @"SELECT ID, CategoryID, Name, UnitID, Price, QuantityAgen, QuantityBanjer, QuantityMalalayang, QuantitySorong FROM Product WHERE ";
// Add the where clauses inside the SQL select query
for (int i = 0; i < queryWords.Length; i++)
{
selectClause += "(Name LIKE '%" + queryWords[i] + "%')";
if (i < queryWords.Length - 1)
selectClause += " AND ";
}
// …Run Code Online (Sandbox Code Playgroud) 我正在查看这个博客,它解释了foreach无需实施IEnumerable. 但是没有深入到实现的细节。
我正在寻找如何在foreach不实施IEnumerable.
编辑:感谢@Sam I am 的评论,我得到了我想要的。(见下面我的回答)
有什么区别
(db.Records as IEnumerable<Record>).Where(...)
Run Code Online (Sandbox Code Playgroud)
和
(db.Records.AsEnumerable()).Where(...).
Run Code Online (Sandbox Code Playgroud)
只是AsEnumerable()快捷方式source as IEnumerable<T>吗?
我已经阅读了https://msdn.microsoft.com/en-us/library/bb335435(v=vs.90).aspx,但没有找到任何关于它的信息.
在C#for IEnumerable中是否有任何扩展方法可以使用谓词返回集合中的项目,直到达到某个给定的限制为止?
例如:
string[] source = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var result = source.Take(2, item => item.EndsWith("e"));
// result == { "one", "three" }
Run Code Online (Sandbox Code Playgroud)
这个例子很容易反映这个想法,但是对于更复杂的逻辑,它可能是有用的,而不是评估集合中的每个项目.
我提出了以下解决方案,但也许.NET中已经有类似的东西了.
请勿使用以下代码 - 仅供参考
public static IEnumerable<T> Take<T>(this IEnumerable<T> enumerable,
int count,
Func<T, bool> predicate)
{
if (enumerable == null)
{
yield break;
}
using (IEnumerator<T> iterator = enumerable.GetEnumerator())
{
var matchingCount = 0;
while (matchingCount < count && iterator.MoveNext())
{
if (predicate(iterator.Current))
{
matchingCount++;
yield return …Run Code Online (Sandbox Code Playgroud) 如何foreach从IEnumerable<Model>.
代码:
public IEnumerable<Model> ListDaftarPjsp()
{
IEnumerable<Model> list = from x in db.PJSPEvaluation
select new Model
{
foo = x.Foo,
bar = x.Bar
};
foreach (Model item in list) {
item.condition = "example";
}
return list;
}
public class Model{
public string foo{ get; set; }
public string bar { get; set; }
public string condition{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了Model. 然后我使用foreach循环结果,然后设置它。但是 Return forcondition仍然没有改变?如何在foreach中设置条件然后返回结果
我有一段看起来很简单的代码,但似乎并非如此。
// Enum values
IEnumerable values = Enum.GetValues(typeof(TVal)).Cast<TVal>();
// Sort them by alpha
if (sortByAlpha)
{
values = (values).OrderBy(i => i.ToString());
}
Run Code Online (Sandbox Code Playgroud)
如果我写:
// Get enum values
IEnumerable values = Enum.GetValues(typeof(TVal)).Cast<TVal>();
// Sort them by alpha
if (sortByAlpha)
{
values = Enum.GetValues(typeof(TVal)).Cast<TVal>().OrderBy(i => i.ToString());
}
Run Code Online (Sandbox Code Playgroud)
有用。
为什么?第一段代码中的值应该相同吗?我没有看到什么?在 .Net 4.5.1 上运行
我创建了一个这样的程序,它运行良好:
using System;
class Program {
static string S(string[] a) => String.Join(',', a);
static void Main() {
string[] a = {"May", "June", "July"};
Console.WriteLine(S(a));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我稍微更改程序:
using System.Linq;
using System;
class Program {
static string S(string[] a) => String.Join(',', a);
static void Main() {
string[] a = {"May", "June", "July"};
var a2 = a.Skip(1);
Console.WriteLine(S(a2));
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error CS1503: Argument 1: cannot convert from
'System.Collections.Generic.IEnumerable<string>' to 'string[]'
Run Code Online (Sandbox Code Playgroud)
然后我尝试将功能更改为:
static string S<T>(T a) => String.Join(',', a);
Run Code Online (Sandbox Code Playgroud)
但我只是得到这个输出:
System.Linq.Enumerable+ListPartition`1[System.String]
Run Code Online (Sandbox Code Playgroud)
编写此函数的正确方法是什么,以便接受两种类型?
编辑:显然偏离主题...转移到Programmers.StackExchange.com.
这不是一个实际问题,它更像是一个谜.
我很想知道是否有办法实现与以下相同的东西,但不使用yield:
IEnumerable<T> Infinite<T>()
{
while (true) { yield return default(T); }
}
Run Code Online (Sandbox Code Playgroud)
yield关键字mscorlib.dll,System.Core.dll?不确定还包括哪些内容).但是,如果您找到一些其他.NET程序集(WPF ?!)的解决方案,我也很感兴趣.我最接近的是:
IEnumerable<int> infinite = null;
infinite = new int[1].SelectMany(x => new int[1].Concat(infinite));
Run Code Online (Sandbox Code Playgroud)
这是"正确的",但在14399次迭代通过可枚举(非常无限)之后会遇到StackOverflowException.
我认为由于CLR 缺乏尾递归优化,可能无法做到这一点.证明会很好:)
是否可以将类转换为IEnumerable?
private SeriesMapping CreateSeriesMapping(string category, string value, string stackKey, CustomObject itemsSource)
{
var data= (IEnumerable)itemsSource;
SeriesMapping successMapping = new SeriesMapping() { SeriesDefinition = new StackedBar100SeriesDefinition() { StackGroupName = stackKey } };
successMapping.ItemMappings.Add(new ItemMapping(category, DataPointMember.XCategory));
successMapping.ItemMappings.Add(new ItemMapping(value, DataPointMember.YValue));
successMapping.ItemsSource = data; // after conversion i did not any data.
// successMapping.ItemsSource = itemsSource; //if i use this i did not get error But while opening the chart Null Reference exception is coming.
return successMapping;
}
Run Code Online (Sandbox Code Playgroud)
这就是我认为班级对象的方式....
我有一个带有索引器的类。我希望能够在this上运行 foreach 循环,但它说我必须实现 Enumerator 接口。这让我实现了两种方法:
#region IEnumerable implementation
public IEnumerator<Type> GetEnumerator() {
throw new NotImplementedException();
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
#endregion
Run Code Online (Sandbox Code Playgroud)
该类是这样构建的:
我希望能够在类中的this上运行 foreach 循环,因此我可以使用索引器的获取和设置逻辑获取和设置数组字段。
这就是为什么我需要实现 GetEnumerator 方法,但我不知道那里需要什么样的实现。
谢谢你的帮助。
注意:我如何实现 IEnumerable<T>线程对我没有帮助,我已经阅读过了。唯一的实现是返回数组的枚举器。
c# ×10
ienumerable ×10
linq ×6
.net ×1
arrays ×1
datagridview ×1
datasource ×1
enumerator ×1
foreach ×1
generics ×1
list ×1
model ×1