检查列表中数组的大小,并将数组中的每个实体显示为唯一的列表值
Check the size of an array inside a list and show each entity in the array as a unique list value
我想知道是否有人可以帮助我,基本上我正在尝试使用 linq 来展平列表。列表内部是一个 phone 数字数组,我想不通的是计算数组的大小并将每个 phone 数字添加为唯一列表值。
var FlatenedList = from x in result
where x.ID != null
orderby x.ID
select new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
BillToPhoneNumber1 = x.BillToContact.PhoneNumbers[0].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[0].Number),
BillToPhoneNumber2 = x.BillToContact.PhoneNumbers[1].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[1].Number),
BillToPhoneNumber3 = x.BillToContact.PhoneNumbers[2].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[2].Number),
};
也许你应该尝试像下面这样的东西,我还没有编译它,但这可能有用
var FlatenedList = from x in result
where x.ID != null
orderby x.ID
select new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
BillToPhoneNumber = x.BillToContact.PhoneNumbers.ToList().Where(num=>num.Number !=null).Select(num=>num)
};
我想你需要一个 Expand Object。
以下是您可以为一件商品执行此操作的方法:
var FlattenedItem = new ExpandoObject();
for(int i = 0; i < x.BillToContact.PhoneNumbers.length; i++) {
// You will have to cast FlattenedItem to an IDictionary to add items
((IDictionary<string, object>)FlattenedItem).Add("BillToPhoneNumber" + i, x.BillToContact.PhoneNumbers[i]);
}
// This is your first phonenumber
var firstPhoneNumber = FlattenedItem.BillToPhoneNumber0;
查看此答案以获取更多信息:
Creating an anonymous type dynamically?
如果我没理解错的话,这就是你需要的:-
var result = accounts.OrderBy(x => x.Id)
.Select(x => new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
PhoneNumbersCount = x.BillToContact.PhoneNumbers.Count(),
//All phone numbers count including null
PhoneNumbersList = x.BillToContact.PhoneNumbers
.Select(z => z.Number ?? String.Empty).ToList()
});
你可以查看这个 Fiddle 我使用了一些样本数据的地方,如果这就是你要找的,请告诉我。
我想知道是否有人可以帮助我,基本上我正在尝试使用 linq 来展平列表。列表内部是一个 phone 数字数组,我想不通的是计算数组的大小并将每个 phone 数字添加为唯一列表值。
var FlatenedList = from x in result
where x.ID != null
orderby x.ID
select new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
BillToPhoneNumber1 = x.BillToContact.PhoneNumbers[0].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[0].Number),
BillToPhoneNumber2 = x.BillToContact.PhoneNumbers[1].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[1].Number),
BillToPhoneNumber3 = x.BillToContact.PhoneNumbers[2].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[2].Number),
};
也许你应该尝试像下面这样的东西,我还没有编译它,但这可能有用
var FlatenedList = from x in result
where x.ID != null
orderby x.ID
select new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
BillToPhoneNumber = x.BillToContact.PhoneNumbers.ToList().Where(num=>num.Number !=null).Select(num=>num)
};
我想你需要一个 Expand Object。
以下是您可以为一件商品执行此操作的方法:
var FlattenedItem = new ExpandoObject();
for(int i = 0; i < x.BillToContact.PhoneNumbers.length; i++) {
// You will have to cast FlattenedItem to an IDictionary to add items
((IDictionary<string, object>)FlattenedItem).Add("BillToPhoneNumber" + i, x.BillToContact.PhoneNumbers[i]);
}
// This is your first phonenumber
var firstPhoneNumber = FlattenedItem.BillToPhoneNumber0;
查看此答案以获取更多信息: Creating an anonymous type dynamically?
如果我没理解错的话,这就是你需要的:-
var result = accounts.OrderBy(x => x.Id)
.Select(x => new
{
AccountNumber = x.AccountNumber,
Balance = x.Balance,
BillToCompanyName = x.BillToContact.CompanyName,
BillToName = x.BillToContact.Name,
PhoneNumbersCount = x.BillToContact.PhoneNumbers.Count(),
//All phone numbers count including null
PhoneNumbersList = x.BillToContact.PhoneNumbers
.Select(z => z.Number ?? String.Empty).ToList()
});
你可以查看这个 Fiddle 我使用了一些样本数据的地方,如果这就是你要找的,请告诉我。