我有一个公共方法
public Task<IEnumerable<MyClass>> GetMyClassObjects()
{
return ??myresult??;
}
Run Code Online (Sandbox Code Playgroud)
以及如何返回正确的结果?
我正在为产品做评论部分,并且在显示评论视图时遇到一些问题。我用 2 个模型制作了模型,以便在一个视图中显示它们,但我将 IEnumerable 显示为列表的方式导致出现错误。我如何修改我的代码才能使其正常工作?
模型
public class DetailsViewModel
{
public Product Product { get; set; }
public IEnumerable<Comments> Comments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器
public ActionResult ProductDetails(int? id)
{
DetailsViewModel detailsViewModel = new DetailsViewModel();
detailsViewModel.Product = db.Product.Find(id);//this is working
detailsViewModel.Comments = db.Comments.Where(c => c.Id_Product == id).OrderBy(x => x.DateComment).ToList();
return View(detailsViewModel);
}
Run Code Online (Sandbox Code Playgroud)
看法
@model Database.Models.DetailsViewModel
<ul>
@Html.DisplayFor(x => x.Comments.DateComment) //getting error here 'IEnumerable<Comments>'
@Html.DisplayFor(x => x.Comments.Comment) // does not containt definiton for 'Comment'
<ul>
Run Code Online (Sandbox Code Playgroud)
我想要得到这样的东西:
我的代码是:
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
namespace IEnumerable
{
public class MyEnumerable<T> : IEnumerable<T>
{
public MyEnumerable(T[] items)
{
this.items = items;
}
public IEnumerator<T> GetEnumerator()
{
return new NestedEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// The enumerator definition.
class NestedEnumerator : IEnumerator<T>
{
public NestedEnumerator(MyEnumerable<T> coll)
{
Monitor.Enter(coll.items.SyncRoot);
this.index = -1;
this.coll = coll;
}
public T Current
{
get { return current; }
}
object IEnumerator.Current
{
get { return Current; }
} …Run Code Online (Sandbox Code Playgroud) 我有两个IEnumerable<StatusGroup>地方StatusGroup像
public class StatusGroup
{
public DateTime ExamDate{ get; set; }
public int SuccessCount{ get; set; }
public int FailCount{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想合并两个IEnumerable,因此每个日期的结果只有一条记录,应该计算计数器属性的总和
例如
List1={Date= 01/01/2016 , Success=10, Fail=0}
List2={Date= 01/01/2016 , Success=0, Fail=3}
Run Code Online (Sandbox Code Playgroud)
结果应该是
List2={Date= 01/01/2016 , Success=10, Fail=3}
Run Code Online (Sandbox Code Playgroud) 我有这个代码用于处理IEnumerable<string>文本中的字符串
foreach (var line in text)
{
var newLine = "";
var ln = line.TrimStart(' ');
var colon = line.IndexOf(":");
if (colon != -1)
{
if (ln.StartsWith("adj")) newLine = "j 1" + line.Substring(colon);
else if (ln.StartsWith("adv")) newLine = "d 1" + line.Substring(colon);
else if (ln.StartsWith("n")) newLine = "n 1" + line.Substring(colon);
else if (ln.StartsWith("v")) newLine = "v 1" + line.Substring(colon);
}
else
{
newLine = line;
}
}
Run Code Online (Sandbox Code Playgroud)
现在它只是循环通过IEnumerable但我想要的是它创建另一个IEnumerable<string>字符串只是内容newLine.Trim()
任何人都可以告诉我如何做到这一点?
文件包含一些字符串,如:
abc 4 2
def 1 1 1
Gh 0
有一些LINQ功能,这个功能的细节并不那么重要
IEnumerable<List<int>> F() {
return File.ReadLines("input.txt")
.Select(a => a.Split(' ').Skip(1).Select(int.Parse).ToList())
.Where(a => a.Count >= 2)
.Take(3).OrderBy(a => a.Sum())
.Select(a => { Console.Write(a[0]); return a; });
}
Run Code Online (Sandbox Code Playgroud)
此功能不会向控制台输出任何内容.为什么?
我如何转换IEnumerable<int>为int[]没有ToArray()?我需要一种比它更快的方法或功能ToArray().我有一个很大的INT大规模的状态和两个小块状的左和右的使用方法的采取和跳过.码:
int[] left = state.Take(pivot).ToArray();
int[] right = state.Skip(pivot).ToArray();
Run Code Online (Sandbox Code Playgroud)
枢轴 - 分裂点.
c# ×7
ienumerable ×7
linq ×3
.net ×1
asp.net-mvc ×1
html ×1
projection ×1
razor ×1
task ×1
toarray ×1