Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq查询中的条件_C#_Asp.net_Linq - Fatal编程技术网

C# linq查询中的条件

C# linq查询中的条件,c#,asp.net,linq,C#,Asp.net,Linq,我有一个linq查询,它从客户和客户联系人获取所有数据。 但有时我并不想要所有联系人,所以我想指定一个条件,如果该值等于get contacts,则运行查询。也差不多 switch (options) { case CustomerOptions.DefaultContacts: break; } 我目前有这个linq查询 var customersToReturn = new ContentList<CustomerServiceModel>() { To

我有一个linq查询,它从客户和客户联系人获取所有数据。 但有时我并不想要所有联系人,所以我想指定一个条件,如果该值等于get contacts,则运行查询。也差不多

switch (options)
{
    case CustomerOptions.DefaultContacts:
    break;
}
我目前有这个linq查询

var customersToReturn = new ContentList<CustomerServiceModel>()
{
    Total = customers.Total,
    List = customers.List.Select(c => new CustomerServiceModel
    {
        Id = c.Id,
        ContractorId = c.ContractorId,
        CompanyName = c.CompanyName,
        Active = c.Active,
        Address = new Address
        {
            Address1 = c.Address1,
            Address2 = c.Address2,
            Address3 = c.Address3,
            Address4 = c.Address4,
        },
        CustomerContacts = c.CustomersContacts.Select(a => new ContactServiceModel
        {
            Name = a.Name,
            Telephone = a.Telephone      
        }).Where(e => e.IsDefault)
    }).ToList()
};
var customersToReturn=newcontentlist()
{
总计=客户。总计,
List=customers.List.Select(c=>newcustomerservicemodel
{
Id=c.Id,
ContractorId=c.ContractorId,
CompanyName=c.CompanyName,
主动的,主动的,
地址=新地址
{
地址1=c.Address1,
地址2=c.Address2,
地址3=c.Address3,
地址4=c.Address4,
},
CustomerContacts=c.CustomerContacts。选择(a=>new ContactServiceModel
{
Name=a.Name,
电话
}).其中(e=>e.IsDefault)
})托利斯先生()
};

是否有一种方法可以设置一个条件,或者我是否需要重复该过程两次—一次只针对客户,另一次针对客户和客户联系人?

如果我理解正确,您希望一些
CustomServiceModel
对象具有
CustomerContacts
,而其他对象没有?那我就这样做

List = customers.List.Select(c => new CustomerServiceModel
                        {
                            Id = c.Id,
                            ContractorId = c.ContractorId,
                            CompanyName = c.CompanyName,
                            Active = c.Active,
                            Address = new Address
                            {
                                Address1 = c.Address1,
                                Address2 = c.Address2,
                                Address3 = c.Address3,
                                Address4 = c.Address4,
                            },
                            CustomerContacts = condition ?
                                c.CustomersContacts.Select(a => new ContactServiceModel
                                {
                                    Name = a.Name,
                                    Telephone = a.Telephone      
                                }).Where(e => e.IsDefault)
                                :null
                        }).ToList()

如果您需要使用switch,请创建一个返回bool的方法,并将其替换为上例中的
条件
短语。

具体条件是什么?