Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
为每行添加两个条目-Linq C#_C#_Linq - Fatal编程技术网

为每行添加两个条目-Linq C#

为每行添加两个条目-Linq C#,c#,linq,C#,Linq,我的问题是: from customer in db.tblCustomers select new { ID = customer.CustomerID, Mobile = customer.Mobile1, LastName = customer.Family }; 对于每个有两部手机的客户,如果第二部手机不为空,我需要添加一个新条目。另外,我应该将第二个条目的姓氏改为类似“second Mobile”的名称。如何使用linq查询从一个客户处获得两个不同的条目

我的问题是:

from customer in db.tblCustomers
select new
{
     ID = customer.CustomerID,
     Mobile = customer.Mobile1,
     LastName = customer.Family
};

对于每个有两部手机的客户,如果第二部手机不为空,我需要添加一个新条目。另外,我应该将第二个条目的姓氏改为类似“second Mobile”的名称。如何使用linq查询从一个客户处获得两个不同的条目?

使用相同的生成类型,不能有一个只有一个电话号码属性,另一个有两个。你可以做:

from customer in db.tblCustomers
select new
{
     ID = customer.CustomerID,
     Mobile = customer.Mobile1,
     SecondMobile = customer.Mobile2, // will be null if no second mobile exists
     LastName = customer.Family
};
否则,您可以创建一个自定义类型
Customer
,该类型将有一个电话号码和一个派生类型
ExtendedCustomer
,其中有两个,然后只实例化其中一个。沿途的一些事情是:

from customer in db.tblCustomers
select customer.Mobile2 != null ? new Customer(...) : new ExtendedCustomer(...);

如果您的意思是在结果集合中有两个不同的对象,则使用union:

List<Customer> result = new List<Customer>();
foreach(var item in db.tblCustomers)
{
    result.Add(new Customer(/*data for first mobile phone*/);
    if(item.Mobile2 != null) 
    {
        result.Add(new Customer(/*data for second mobile phone*/);
    }
}
列表结果=新列表();
foreach(数据库TBL客户中的var项目)
{
结果。添加(新客户(/*第一部手机的数据*/);
如果(item.Mobile2!=null)
{
结果.添加(新客户(/*第二部手机的数据*/);
}
}

使用相同的生成类型,您不能让一个类型只有一个电话号码属性,另一个类型有两个。您可以执行以下操作:

from customer in db.tblCustomers
select new
{
     ID = customer.CustomerID,
     Mobile = customer.Mobile1,
     SecondMobile = customer.Mobile2, // will be null if no second mobile exists
     LastName = customer.Family
};
否则,您可以创建一个自定义类型
Customer
,该类型将有一个电话号码和一个派生类型
ExtendedCustomer
,两个电话号码-并仅实例化一个或另一个。psudo中的一些内容:

from customer in db.tblCustomers
select customer.Mobile2 != null ? new Customer(...) : new ExtendedCustomer(...);

如果您的意思是在结果集合中有两个不同的对象,则使用union:

List<Customer> result = new List<Customer>();
foreach(var item in db.tblCustomers)
{
    result.Add(new Customer(/*data for first mobile phone*/);
    if(item.Mobile2 != null) 
    {
        result.Add(new Customer(/*data for second mobile phone*/);
    }
}
列表结果=新列表();
foreach(数据库TBL客户中的var项目)
{
结果。添加(新客户(/*第一部手机的数据*/);
如果(item.Mobile2!=null)
{
结果.添加(新客户(/*第二部手机的数据*/);
}
}

如果有帮助的话,你能试试这个吗

var customers = db.tblCustomers.SelectMany(x => x.GetMultipleRow()).ToList();
GetMultipleRow是一种扩展方法,如下所示

public static class CustomerExtensions
{
    public static IEnumerable<Customer> GetMultipleRow(this Customer cust)
    {
        yield return new Customer { CustomerID = cust.CustomerID, Mobile1 = cust.Mobile1, Family = cust.Family };
                                    /* Data for first mobile*/
        if (cust.Mobile2 != null)
            yield return new Customer { CustomerID = cust.CustomerID, Mobile1 = cust.Mobile2, Family = cust.Family };
                                        /* Data for second mobile*/
    }
}
公共静态类CustomerExtensions
{
公共静态IEnumerable GetMultipleRow(此客户客户)
{
收益返回新客户{CustomerID=cust.CustomerID,Mobile1=cust.Mobile1,Family=cust.Family};
/*第一部手机的数据*/
如果(cust.Mobile2!=null)
收益返回新客户{CustomerID=cust.CustomerID,Mobile1=cust.Mobile2,Family=cust.Family};
/*第二部手机的数据*/
}
}

如果有帮助的话,你能试试这个吗

var customers = db.tblCustomers.SelectMany(x => x.GetMultipleRow()).ToList();
GetMultipleRow是一种扩展方法,如下所示

public static class CustomerExtensions
{
    public static IEnumerable<Customer> GetMultipleRow(this Customer cust)
    {
        yield return new Customer { CustomerID = cust.CustomerID, Mobile1 = cust.Mobile1, Family = cust.Family };
                                    /* Data for first mobile*/
        if (cust.Mobile2 != null)
            yield return new Customer { CustomerID = cust.CustomerID, Mobile1 = cust.Mobile2, Family = cust.Family };
                                        /* Data for second mobile*/
    }
}
公共静态类CustomerExtensions
{
公共静态IEnumerable GetMultipleRow(此客户客户)
{
收益返回新客户{CustomerID=cust.CustomerID,Mobile1=cust.Mobile1,Family=cust.Family};
/*第一部手机的数据*/
如果(cust.Mobile2!=null)
收益返回新客户{CustomerID=cust.CustomerID,Mobile1=cust.Mobile2,Family=cust.Family};
/*第二部手机的数据*/
}
}

正如您所说,他需要为第二个手机号码设置另一个字段或设置一个parentId字段,无论哪种方式,他所做的都是错误的。您是对的=)没关系。我不认为它解决了给定的问题。为什么被接受?下面是正确的解决方案,因为你说他需要第二个手机号码的另一个字段或有一个parentId字段,无论哪种方式,他所做的都是错的。你是对的=)没关系。我认为它解决不了给定的问题。为什么被接受?下面是正确的解决方案