使用linq的类中的数据

时间:2015-12-04 13:53:52

标签: c# linq

public partial class DT_createdRentalObject {

    private SimpleData masterDataField; -> Name

    private Address[] addressField; -> AddressName

    private Association[] associationField; -> AssociationName, AssociationId

}

如何用LINQ编写

 public string createdRentalObject(DT_createdRentalObject ro)
{
 var list = (from s in ro ...?
}

此结构:

|名称| | AddressName | | AssociationName | | AssociationId |

Rafal AFRICA TS 1

Rafal USA TA 2

Rafal GERMANY TS 1

Rafal FRANCE TA 2

3 个答案:

答案 0 :(得分:1)

假设您有一个createdRentalObjects列表,例如List<createdRentalObject>

public string createdRentalObject(**List<DT_createdRentalObject**> ro)
{
  List<createdRentalObject> aList = ro.Where(s=> s.masterDataField == "nameX").FirstOrDefault();
  // would give you a list where the masterDataField equals nameX
}

下载Linqpad https://www.linqpad.net/-并浏览示例

答案 1 :(得分:1)

尝试tis

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            var list = test.rentalObjects.Select(x => new
            {
                Name = x.masterDataField.data,
                AddressName = x.addressField.FirstOrDefault().address,
                AssociationName = x.associationField.FirstOrDefault().AssociationName,
                AssociationId = x.associationField.FirstOrDefault().AssociationId
            }).ToList();
        }
    }
    public class Test
    {
        public List<DT_createdRentalObject> rentalObjects { get; set; }
        public Test()
        {
            rentalObjects = new List<DT_createdRentalObject>() {
                new DT_createdRentalObject() {
                    masterDataField = new SimpleData() { data = "Rafal"},
                    addressField = new Address[] { new Address() {  address = "AFRICA" }},
                    associationField = new Association[] { new Association() {  AssociationName = "TS",   AssociationId = "1" }}
                },
                new DT_createdRentalObject() {
                    masterDataField = new SimpleData() { data = "Rafal"},
                    addressField = new Address[] { new Address() {  address = "USA" }},
                    associationField = new Association[] { new Association() {  AssociationName = "TA",   AssociationId = "2" }}
                },
                new DT_createdRentalObject() {
                    masterDataField = new SimpleData() { data = "Rafal"},
                    addressField = new Address[] { new Address() {  address = "GERMANY" }},
                    associationField = new Association[] { new Association() {  AssociationName = "TS",   AssociationId = "1" }}
                },
                new DT_createdRentalObject() {
                    masterDataField = new SimpleData() { data = "Rafal"},
                    addressField = new Address[] { new Address() {  address = "FRANCE" }},
                    associationField = new Association[] { new Association() {  AssociationName = "TA",   AssociationId = "2" }}
                },
            };

        }

        public partial class DT_createdRentalObject
        {
            public SimpleData masterDataField { get; set; } //-> Name
            public Address[] addressField { get; set; } //-> AddressName
            public Association[] associationField { get; set; } //-> AssociationName, AssociationId
        }
        public class SimpleData
        {
            public string data { get; set; }
        }
        public class Address
        {
            public string address { get; set; }
        }
        public class Association
        {
            public string AssociationName { get; set; }
            public string AssociationId { get; set; }
        }

    }
}
​

答案 2 :(得分:1)

由于您的示例数据不包含地址和关联的所有组合,因此我假设它们应该通过索引进行配对。 Zip是一种方法:

var list = 
    ro.addressField.Zip(ro.associationField, 
                        (add, asn) => new {ro.masterDataField.Name,
                                           add.AddressName,
                                           asn.AssociationName,
                                           asn.AssociationId}
                       );

请注意,必须从 DT_createdRentalObject类中完成,因为这些字段是私有的。