这是生成列表的linq
var query = (from vinfo in vQuery
join Add in _db.Addresses on vinfo.Id equals Add.vId
select new AddressLabels
{
Id = vinfo.Id,
Name = vinfo.Name,
Address1 = Add.Address1,
Address2 = Add.Address2,
ZipCode = Add.ZipCode,
City = Add.City,
Country = Add.Country,
Contact1 = Add.Contact1,
Contact2 = Add.Contact2
});
在上面的列表中,contact1和contact2都有可能相同。 当Contact1不等于contact2时,我必须为上面列表中的每个联系人生成2个单独的列表。 列表将具有与父列表相同的数据,但一个列表将具有contact1,另一个列表将具有contact2 这就是我所看到的。
List1:
Id
Name
Address1
Address2
ZipCode
City
Country
Contact1
列表2:
Id
Name
Address1
Address2
ZipCode
City
Country
Contact2
请帮助
答案 0 :(得分:0)
// Check if your list has items with same contacts
if (!query.Any(a => a.Contact1.Equals(a.Contact2)))
{
return query;
}
// Else, create two new lists
var itemsWithSameContacts = query.Where(a => a.Contact1.Equals(a.Contact2));
var firstList = itemsWithSameContacts.Select(a => new AddressLabels
{
Id = a.Id,
Name = a.Name,
Address1 = a.Address1,
Address2 = a.Address2,
ZipCode = a.ZipCode,
City = a.City,
Country = a.Country,
Contact1 = a.Contact1,
}).ToList();
var secondList = itemsWithSameContacts.Select(a => new AddressLabels
{
Id = a.Id,
Name = a.Name,
Address1 = a.Address1,
Address2 = a.Address2,
ZipCode = a.ZipCode,
City = a.City,
Country = a.Country,
Contact2 = a.Contact2,
}).ToList();
// If you want these lists also to have contacts
// which don't have same contacts, then you can also
var itemsWithNotSameContacts = query.Where(a => !a.Contact1.Equals(a.Contact2));
firstList.AddRange(itemsWithNotSameContacts);
secondList.AddRange(itemsWithNotSameContacts);