English 中文(简体)
使用 LINQ 从两个 IIE 选择数字
原标题:Select from two IEnumerables using LINQ

This seems fairly easy but I can t focus.
Let s say I have got 2 classes defined like this:

class Class1
{
    public int Document;
    public decimal Amount;
}

class Class2
{
    public string Document;
    public decimal Amount;
}

IEnumberable<Class1> 结果1 >IEnumberable<Class2> 结果2 定义并填满了数据。

我有一个三等的 定义像这样:

class ResultClass
{
    public string Document;
    public decimal? AmountFromClass1;
    public decimal? AmountFromClass2;
}

I want a LINQ query which selects some fields from the first result and some fields from the second result and puts them in a third result defined as List<ResultClass> result.
For example, if I have the first result (result1) populated like this:

  Document    Amount
     1         1.55
     2         2.55

和第二个结果 ( 结果2 ), 人口组成像这样 :

  Document    Amount
   "d-1"        1.22
   "d-2"        2.22

我希望最终的 结果 包含类似的东西( 顺序不重要) :

  Document    AmountFromClass1    AmountFromClass2
    "1"             1.55                null
   "d-1"            null                1.22
    "2"             2.55                null
   "d-2"            null                2.22

目前我使用两个 < code> foreach 语句来做此操作, 例如 :

List<ResultClass> result = new List<ResultClass>();

foreach (var o in result1)
    result.Add(new ResultClass 
    { 
        Document = o.Document.ToString(), 
        AmountFromClass1 = o.Amount, 
        AmountFromClass2 = null 
    });

foreach (var o in result2)
    result.Add(new ResultClass 
    { 
        Document = o.Document, 
        AmountFromClass1 = null, 
        AmountFromClass2 = o.Amount 
    });

But I would like to do this using LINQ.
Is it possible?

EDIT: TO REFINE THE QUESTION (also added the entity-framework tag)
As this is just a code example, I am aware that there might be no real benefits of doing this in a single LINQ statement as it would do the same.
However, I am looking for a performance improvement where result1 and result2 are actually IQueryables from LINQ-to-Entities, in a way so I could do something like this:

List<ResultClass> MyFunc()
{
    Database DB = new Database(); // object context
    var result1 = DB.Class1s.Where(...);
    var result2 = DB.Class2s.Where(...);
    return SomeMagic()
        .Select(x => new ResultFunc { ... })
        .ToList();
}
最佳回答

在两个收藏项中,每个项目的项目都包含在 ResultClasss 对象上,并将由此得出的两个数字相混合:

var query1 = from o in result1
             select new ResultClass
             {
                 Document = o.Document.ToString(),
                 AmountFromClass1 = o.Amount
             };

var query2 = from o in result2
             select new ResultClass
             {
                 Document = o.Document,
                 AmountFromClass2 = o.Amount
             };

var result = query1.Concat(query2).ToList();
问题回答

这是您转换成 LINQ 的代码

var result = result1
  .Select(c => new ResultClass() { Document = c.Document.ToString(), AmountFromClass1 = c.Amount })
  .Concat(result2.Select(c => new ResultClass() { Document = c.Document, AmountFromClass2 = c.Amount }))
  .ToList();

根据你需要的输出 似乎你需要一个联盟

        var class1 = new List<Class1>();
        var class2 = new List<Class2>();
        class1.Add(new Class1 (){Document=1, Amount = 1.0M});
        class1.Add(new Class1 (){Document=2, Amount = 1.0M});
        class2.Add(new Class2 (){Document="1", Amount = 1.0M});
        class2.Add(new Class2 (){Document="3", Amount = 1.0M});
            var result = class1.Select(x=> new ResultClass{Document = x.Document.ToString(), AmountFromClass1=x.Amount, AmountFromClass2 = null}).Union(
                            class2.Select(x=> new ResultClass{Document = x.Document, AmountFromClass2=x.Amount, AmountFromClass1 = null})   );

如果您想要交叉加入, 您可以使用含有多个条款的查询语法 。 < a href="https:// stackoverflow.com/ questions/ 4076319/ linq- cross-join- query- for- 免责- list" >Example





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签