我想得到Item对象的集合或列表,但我现在得到数组的数组.
Item[][] s = employees.Select(e => e.Orders.Select(o => new Item(e.ID, o.ID)).ToArray()).ToArray();
Run Code Online (Sandbox Code Playgroud)
任何人都可以选择我的解决方案吗?
PS只是LinQ解决方案:)
在给定的代码示例中,为什么我们使用x.ToArray()?每个元素x已经是一个数组了不是吗?
请让我少搞糊涂:)
var array1 = new int[3] { 1, 2, 3 }; //New integer array
var array2 = new int[3] { 4, 5, 6 }; //New integer array
var array3 = new int[3] { 7, 8, 9 }; //New integer array
IList<int[]> list4 = new List<int[]> { array1, array2, array3 };
var theManyList = list4.SelectMany(x => x.ToArray()).ToList();
Run Code Online (Sandbox Code Playgroud) 好日子,
假设我有一个静态List<AClass>对象(让我们将其命名为myStaticList),其中包含其他列表,该列表包含具有CId和Name属性的其他列表.
我需要做的是
foreach(AClass a in myStaticList)
{
foreach(BClass b in a.bList)
{
foreach(CClass c in b.cList)
{
if(c.CId == 12345)
{
c.Name = "Specific element in static list is now changed.";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用LINQ Lambda表达式实现此目的吗?
就像是;
myStaticList
.Where(a=>a.bList
.Where(b=>b.cList
.Where(c=>c.CId == 12345) != null) != null)
.something logical
.Name = "Specific element in static list is now changed.";
Run Code Online (Sandbox Code Playgroud)
请注意,我想更改静态列表中该特定项的属性.
我需要迭代Linq从Dictionary返回的列表.但是,如果我使用ToList(),它将成为列表的列表.
Dictionary<Tuple<double, double>, List<Tuple<double, double>>> dict = new Dictionary<Tuple<double,double>,List<Tuple<double,double>>>();
Random rnd = new Random();
for(int x =0; x < 3 ; ++x)
{
double a = rnd.NextDouble()*100;
double b = rnd.NextDouble()*100;
double c = Math.Ceiling(rnd.NextDouble()*100);
double d = Math.Ceiling(rnd.NextDouble()*100);
Tuple<double, double> tp = new Tuple<double, double>(c, d);
List<Tuple<double, double>> ll = new List<Tuple<double,double>>();
ll.Add(tp);
c = Math.Ceiling(rnd.NextDouble() * 100);
d = Math.Ceiling(rnd.NextDouble() * 100);
tp = new Tuple<double, double>(c, d);
ll.Add(tp);
dict.Add(new Tuple<double, double>(a, b), ll);
}
var …Run Code Online (Sandbox Code Playgroud) 我有一个项目列表('sections'),它有一个getter和一个setter.这些"部分"中的每一个都有一个"项目"列表.我想提供一个yield return属性来将'Items'属性公开为包含类的IEnumerable.我只是不能正确地得到语法.我在这里需要另一个循环还是会迭代太多次?
public virtual IList<ISection> Sections{ get; set; }
public virtual IEnumerable<IItem> Sections {
get{
foreach (var sect in Sections) {
// will this iterate too many times if I add an additional loop?
yield return sect.Items
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在这样做,但它不起作用(不编译),我需要先 GroupBy 然后 OrderBy。
lstOrderItems.OrderBy(x => x.itemPrice).GroupBy(p => p.CatID);
Run Code Online (Sandbox Code Playgroud) 我是LINQ lambda表达的新手,关于下面的问题,我已经被困了一段时间.我想执行左外连接并且想要选择左表而不是右表但是当我选择左表时,下面的查询给出了错误
"查询"是IQueryable,也是"model2"

var model = query.GroupJoin(model2,
o => o.plu,
m => m.plu,
(o, m) => new
{
SmartCoupon = o,
Product = m.DefaultIfEmpty(),
})
.SelectMany
(
a => a.SmartCoupon
);
Run Code Online (Sandbox Code Playgroud)
下面是正确的查询与右表,但我需要左表
var model = query.GroupJoin(model2,
o => o.plu,
m => m.plu,
(o, m) => new
{
SmartCoupon = o,
Product = m.DefaultIfEmpty(),
})
.SelectMany
(
a => a.Product
);
Run Code Online (Sandbox Code Playgroud) 考虑一下你有一个集合的场景,在这个集合中是特定的对象。这些对象也包含一个集合,并且在这些集合中包含更多相同的对象。它是一个具有许多层的嵌套集合。
List<WorkItemClassificationNode> items;
List<WorkItemClassificationNode> subItems = items.Children;
List<WorkItemClassificationNode> subSubItems = subItems.Children;
// etc
Run Code Online (Sandbox Code Playgroud)
我只想要一种方法来遍历每个层,以便对每个项目应用相同的逻辑,但我想不出一种直接的方法来做到这一点,而无需编写大量嵌套循环。
我需要从列表中选择特定的两列,并为两个组合框加载这些值.我试图使用LINQ这样做,它传递给我一个错误"无法将类型的对象强制转换为'类型'System.Collections.Generic.List".
这是我的方法,
private void getVehicleNo()
{
List<Exp_VehicleDTO> oData = oVehicleBL.VehiclesSearch(Convert.ToInt32(Session["CompanyID"].ToString()));
oData = ((List<Exp_VehicleDTO>)oData.SelectMany(x => x.VehicleNo)).ToList();
ddVehicleNo.DataSource = oData;
ddVehicleNo.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
这是车辆搜索方法,
public List<Exp_VehicleDTO> VehiclesSearch(int companyId)
{
List<Exp_VehicleDTO> results = new List<Exp_VehicleDTO>();
try
{
using (CloudConnection oCloudConnection = new CloudConnection(DMSSWE.Common.ConnectionString))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" SELECT ");
sb.AppendLine("CompanyID,");
sb.AppendLine("VehicleId,");
sb.AppendLine("VehicleType,");
sb.AppendLine("VehicleNo,");
sb.AppendLine("Status,");
sb.AppendLine("CreatedDateTime,");
sb.AppendLine("CreatedBy,");
sb.AppendLine("CreatedMachine ");
sb.AppendLine(" FROM Exp_CompanyVehicle ");
sb.AppendLine(" WHERE 1=1 ");
sb.AppendLine(" AND (CompanyID=?CompanyID)");
oCloudConnection.CommandText = sb.ToString();
oCloudConnection.Parameters.Clear();
oCloudConnection.Parameters.Add(new Parameter { Name = "CompanyID", Value = …Run Code Online (Sandbox Code Playgroud) 我遇到了SelectMany表达式的一个问题,我无法解决这个问题.
考虑一下:我有这个类的对象的集合
class Tag
{
string DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试获取所有显示文本(实际上是更复杂表达式的一部分):
var texts = AvailableTags.SelectMany(t => t.DisplayText);
Run Code Online (Sandbox Code Playgroud)
现在为什么这会给我一个IEnumerable<char>而不是一个IEnumerable<string>??? 我错过了什么吗?