在LINQ GroupBy子句

时间:2018-08-06 11:35:16

标签: c# linq

嘿,我想问一下是否可以在GroupBy子句中使用List对象属性:

我的问题:

我想直接通过列表和对象的属性对列表进行分组 该列表包含的内容

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

namespace ConsoleApp8
{

class ExampleDocument
{
    public int idDoc { get; set; }
    public List<Payments> payments { get; set; }

    public string date { get; set; }

    public ExampleDocument(int a, List<Payments> paymentss,string d)
    {
        idDoc = a;
        payments = paymentss;
        d = date;
    }
}
class Payments
{
    public int value { get; set; }
    public string method { get; set; }

    public Payments(int a, string b)
    {
        value = a;
        method = b;
    }
}
class Program
{
    List<ExampleDocument> exampleList = new List<ExampleDocument>();
    List<Payments> listOfPayments = new List<Payments>();
    Payments a = new Payments(1000, "cash");
    Payments b = new Payments(2000, "card");

    void Work()
    {
        listOfPayments.Add(a);
        listOfPayments.Add(b);
        exampleList.Add(new ExampleDocument(1, listOfPayments,"2018-08-06"));
    }

    static void Main(string[] args)
    {
        Program c = new Program();

        c.exampleList.GroupBy(p => new { p.date, ((Payments)p.payments).method });
    }
}

}

“(((付款)p.payments).method}”在这里,我有例外

  

无法转换类型   'System.Collections.Generic.List'到   “ ConsoleApp8.Payments”

  1. ExampleList-包含商业文档。
  2. p.Example_VARIABLE-是文档的发布日期。
  3. 付款是包含付款属性的类
  4. 付款是所有付款的清单(在文档中)
  5. 方法是一种付款方式

当Cetin Bastoz回答时,我管理了新查询

namespace ConsoleApp8

{

class ExampleDocument
{
    public int idDoc { get; set; }
    public int DocNumber { get; set; }
    public List<Payments> payments { get; set; }
    public string date { get; set; }

    public ExampleDocument(int a, int docNumber, List<Payments> paymentss, string d)
    {
        idDoc = a;
        payments = paymentss;
        DocNumber = docNumber;
        date = d;
    }
}
class Payments
{
    public int payID { get; set; }
    public int value { get; set; }
    public string method { get; set; }

    public Payments(int PayID, int a, string b)
    {
        payID = PayID;
        value = a;
        method = b;
    }
}
class Program
{
    List<ExampleDocument> exampleList = new List<ExampleDocument>();
    List<Payments> listOfPayments = new List<Payments>();

    void Work()
    {
        Payments a = new Payments(2, 1000, "cash");
        Payments b = new Payments(1, 2000, "card");

        listOfPayments.Add(a);
        listOfPayments.Add(b);
        exampleList.Add(new ExampleDocument(1, 20189, listOfPayments, "2018-08-06"));
        exampleList.Add(new ExampleDocument(1, 201810, listOfPayments, "2018-08-08"));
        exampleList.Add(new ExampleDocument(1, 201811, listOfPayments, "2018-08-09"));
    }

    static void Main(string[] args)
    {
        Program c = new Program();
        c.Work();
        var data2 = from d in c.exampleList
                    from p in d.payments
                    select new
                    {
                        Number = d.DocNumber,
                        DocDate = d.date,
                        payID = p.payID,
                        Amount = p.value
                    } into s
                    group s by s.DocDate into g
                    select g;

        var sDocDate = "2018-08-06";
        var result = data2.Select(x => x.Where(p => p.DocDate.Equals(sDocDate)));

        Console.ReadKey();
    }
}

} 一切都清楚了,直到我想将date2结果与字符串

给出的日期进行比较
var sDocDate = "2018-01-21"
var result = data2.Select(x => x.Where(p => p.DocDate.Equals(sDocDate)));

Why at bottom image "result" got those "1,2,3 variable without entries"

1 个答案:

答案 0 :(得分:0)

您将使用OfType <>()从ArrayList中获取ExampleObject类型(我以为没有人仍然使用ArrayList):

var grouped = exampleList.OfType<ExampleObject>()
   .GroupBy(p => new { p.EXAMPLE_VARIABLE, p.ExampleProperty});

编辑:尽管您的代码有错误,但在更正之后:

var data = from d in exampleList
           from p in d.payments
           group p by new {DocDate=d.date, PayType=p.method} into g
           select g;