标签: linq-to-objects

操作方法:使用 Linq to 对象连接三个列表

问题是地址没有输出

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace LinqToObjects
          {
            class Program
          {
    static void Main(string[] args)
    {
        var customers = Customer.GetAllCustomers();
        var addresses = Address.GetAllAddresses();
        var addressRelations = AddressRelation.GetAllAddressRelations();

        var results = customers
                    .Join(addressRelations,
                    c => c.CustomerID,
                    ar => ar.CustomerID,
                    (c, ar) => new
                    {
                        CustomerName = c.FirstName + " " + c.LastName,
                        CustomerID = c.CustomerID,
                        AddressRelID = ar.AddressID
                    });
        var resultsJoined = results
                       .GroupJoin(addresses,
                        ar => ar.AddressRelID,
                        a => a.AddressID,
                        (ar, a) => new
                        {
                            CustomerName …
Run Code Online (Sandbox Code Playgroud)

linq linq-to-objects c#-4.0

1
推荐指数
1
解决办法
5285
查看次数

vb.net 中 linq 运算符不等于

我有两个列表,即 csvlist 和 emplist csvlist 是一个列表(CSV),xml 是一个列表(xml)

我正在尝试编写一个 linq 查询,如果 csv.id 不等于 xml.id,它会更新 csv.attr。但是,在 vb.net 中我不知道不等于的运算符。

在 C# 中,您可以使用 != 但在 vb.net> 中等效的是什么

public class CSV
   public property id as string
   public property attr as string
end class 

public class XML
   public property id as string
   public property attr as string
end class  

Dim csvlist as List(of CSV)
Dim xmllist as List(of XML)


Dim Query  = from csv in csvlist, xml in xmllist
                  where csv.id != xml.id
                  select xml
Run Code Online (Sandbox Code Playgroud)

Id …

linq vb.net linq-to-objects

1
推荐指数
1
解决办法
5870
查看次数

与LINQ有些混淆

一些背景信息;

  • LanguageResource是基类
  • LanguageTranslatorResource和LanguageEditorResource继承自LanguageResource
  • LanguageEditorResource定义了一个IsDirty属性
  • LanguageResourceCollection是LanguageResource的集合
  • LanguageResourceCollection在内部保存LanguageResources Dictionary<string, LanguageResource> _dict
  • LanguageResourceCollection.GetEnumerator()返回 _dict.Values.GetEnumerator()

我有一个LanguageResourceCollection _resources只包含LanguageEditorResource对象,并希望使用LINQ来枚举那些脏的,所以我尝试了以下内容.我的具体问题是粗体.

  1. _resources.Where(r => (r as LanguageEditorResource).IsDirty)

    但是,Intellisense没有显示其他LINQ方法,但我仍然对它进行编码,并告诉我"LanguageResourceCollection不包含'Where'的定义,也没有扩展方法......".

    为什么LanguageResourceCollection实现IEnumerable的方式阻止它支持LINQ?

  2. 如果我将查询更改为

    (_resources as IEnumerable<LanguageEditorResource>).Where(r => r.IsDirty)

    Intellisense,则显示LINQ方法和解决方案.但是在运行时我得到一个ArgumentNullException"Value不能为null.参数名称:source".

    这是我的LINQ代码中的问题吗?
    这是一般类设计的问题吗?
    我如何深入了解LINQ生成的内容以尝试查看问题所在?

我对这个问题的目的不是为特定问题找到解决方案,因为我现在必须使用其他(非LINQ)方法解决它,而是尝试提高我对LINQ的理解并学习如何改进设计我的课程与LINQ更好地合作.

.net c# linq linq-to-objects

0
推荐指数
1
解决办法
2640
查看次数

使用LINQ比较两个列表

如果我有2个字符串列表:

List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "111", "999");
Run Code Online (Sandbox Code Playgroud)

如何比较列表中每个项目中的每个字符?例如:应将"0"与"0","1"与"1","0"与"0"进行比较,依此类推.似乎我可以使用,SelectMany但我坚持如何做到这一点

编辑:

这些列表在相互比较时应该返回true(因为星号表示任何字符,我正在验证以确保每个项目的长度恰好为3个字符)

List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "1*1", "***");
Run Code Online (Sandbox Code Playgroud)

linq asp.net linq-to-objects

0
推荐指数
1
解决办法
1441
查看次数

列出lambda表达式,为一个特定查询抛出InvalidOperationException

我有这个片段.

List<Frames> FrameList;
Run Code Online (Sandbox Code Playgroud)

其中Frames是一个只包含字符串的类,包括字符串字段"ExerciseID".

...


void GetFramesForExercise(string exerciseID)

    ....

    if (exerciseID == "3.2.2") { 
       Console.Write(""); }  // quick and dirty to add a breakpoint

    if (FramesList[115].ExerciseID.Equals(exerciseID)) { 
       Console.Write(""); } // quick and dirty to add a breakpoint

    frames = (Frames)FramesList.Single(r => r.ExerciseID.Equals(exerciseID));
Run Code Online (Sandbox Code Playgroud)

通过在console.write语句上添加断点,我能够看到exerciseID确实等于"3.2.2"并且FramesList [115]指向一个ID等于"3.2.2"的Exercise实例.指向的实例正确初始化.

为什么我的查询会抛出InvalidOperationException?

c# linq-to-objects

0
推荐指数
1
解决办法
327
查看次数

LINQ(to Object)查询是否可能包含无限循环?

我有一个简单的课程:

public class RawBomItem
{
    private string material;
    private string item;
    private string component;
    private string quantity;
    private string b;
    private string spt;
...
}
Run Code Online (Sandbox Code Playgroud)

每个数据库都有一个属性.

然后我有一个List包含这个类的实例

    private List<RawBomItem> rawBom;
Run Code Online (Sandbox Code Playgroud)

该列表包含超过70000个项目.

此时我想在此List上运行一个复杂的LINQ查询.

List<string> endProducts = new List<string>(
    rawBom.Where(x1 => new List<string>(rawBom.Select(x2 => x2.Component)
                                              .Distinct())
                           .Contains(x1.Material) && (x1.B != "F"))
          .Select(x3 => x3.Material));
Run Code Online (Sandbox Code Playgroud)

查询似乎遇到了无限循环.(我等了好几分钟然后把它关了)

我会把它变成DB工作,我只是对可能出现的问题感兴趣.

c# linq linq-to-objects

0
推荐指数
1
解决办法
577
查看次数

为什么我的LINQ OrderBys不起作用?

这个LINQ:

public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd)
{
    // Break the doubles into their component parts:
    int deptStartWhole = (int)Math.Truncate(deptBegin);
    int startFraction = (int)((deptBegin - deptStartWhole) * 100);
    int deptEndWhole = (int)Math.Truncate(deptEnd);
    int endFraction = (int)((deptBegin - deptEndWhole) * 100);

    return inventoryItems.Where(d => d.dept >= deptStartWhole).Where(e => e.subdept >= startFraction)
        .Where(f => f.dept <= deptEndWhole)
        .Where(g => g.subdept >= endFraction)
        .OrderBy(o => o.dept)
        .OrderBy(s => s.subdept);
}
Run Code Online (Sandbox Code Playgroud)

...当我输入时返回预期的数据:

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/79.99/
Run Code Online (Sandbox Code Playgroud)

...即(子集):

<InventoryItem>
<Description>LID - Blank & Ten PLU</Description>
<ID>110</ID>
<OpenQty>0</OpenQty>
<UPC>110</UPC> …
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable linq-to-objects

0
推荐指数
1
解决办法
91
查看次数

调用IEnumerable.Cast <T>()会将基础类型更改为其他类型,还是保留基础类型?

说我有 ISet<T> _set = new HashSet<T>();

现在,如果我这样做:( _set.Cast<TInterface>().Contains(obj, comparer);哪里有T工具TInterface),我是否会放弃它的O(1)好处HashSet<T>

换句话说 - 是否.Cast<T>()将基础类型(HashSet<T>在本例中)更改为其他类型,或保留基础类型?

c# linq linq-to-objects

0
推荐指数
1
解决办法
98
查看次数

LINQ OrderBy不起作用

我有以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderByElementSubelement
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Place> places = new List<Place>();
            places.Add(new Place { Address = "Fifth Street", Score = 29, Houses = new List<House> { new House { Owner = "Mike", Score = 32 }, new House { Owner = "Bobby", Score = 3 } } });
            places.Add(new Place { Address = "Sixth Street", Score = 29, Houses = new List<House> { new …
Run Code Online (Sandbox Code Playgroud)

.net c# linq linq-to-objects exception

0
推荐指数
1
解决办法
633
查看次数

使用linq计算列表属性的总和,不包括最小值和最大值

这是我到目前为止:

decimal? total = list.Sum(item => item.Score);
Run Code Online (Sandbox Code Playgroud)

我想要做的是排除列表中的最小值和最大值,然后获取总值.

是否可以在一个linq语句中完成所有这些操作?

c# linq linq-to-objects list

0
推荐指数
2
解决办法
1438
查看次数

标签 统计

linq-to-objects ×10

linq ×9

c# ×7

.net ×2

asp.net ×1

c#-4.0 ×1

exception ×1

ienumerable ×1

list ×1

vb.net ×1