当条目为零/空时,如何处理一个对多(或零)实体的迭代? [英] How do I deal with iterating over a one to many (or zero) entity when there are zero/null entry?
问题描述
我不断得到NullReferenceException
尝试遍历一行中的空条目.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public int AddressID { get; set; }
}
public class Address
{
public int ID { get; set; }
public string FirstLine { get; set; }
public string SecondLine { get; set; }
}
使用默认支架模板_context.Person.Include(c => c.Address);
无论是否有地址,视图都可以正常工作.
但是,我现在将默认索引页面替换为更新的网格,该网格通过具有过滤和分页机制的AJAX加载.为此,我需要将数据转换为我的ViewModel
我希望能够以文本形式在线显示地址.我尝试了以下方法:
var tmp = _context.Person.Include(x => x.Address).ToList();
tmp.ForEach(x => vm.List.Add(new IndexListItem()
{
Name = x.Name,
Address = x.Address.FirstLine + " " + x.Address.SecondLine,
ID = x.ID
}));
但是,在调试时,即使行上有数据,地址也似乎总是为空.
我很确定我可以在标准的foreach循环上执行此操作,并执行if空检查,但是,我忍不住觉得这很简单,而且已经使它复杂化了./p>
有没有更简单的方法来返回我需要的地址详细信息?
正如我在评论中所说,您应该在一条语句中这样做:
vm.List = _context.Person
.Select(p => new IndexListItem
{
Name = p.Name,
Address = p.Address.FirstLine + " " + p.Address.SecondLine,
ID = p.ID
}).ToList();
这有两个优点:
- 它已翻译成SQL,因此仅从数据库中提取了四个必填字段(这与更宽的记录有很大的区别).
- EF将生成将空值考虑在内的SQL.
如果您不希望使用" "
,则可以...
Address = (p.Address.FirstLine + " " + p.Address.SecondLine).Trim()
...或...
Address = p.Address.FirstLine != null ? p.Address.FirstLine + " " : "")
+ p.Address.SecondLine
但是我认为查看数据并不重要.
I keep getting NullReferenceException
trying to iterate over a null entry on a row.
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public int AddressID { get; set; }
}
public class Address
{
public int ID { get; set; }
public string FirstLine { get; set; }
public string SecondLine { get; set; }
}
With the default scaffold template, _context.Person.Include(c => c.Address);
The view works fine if there is an address or not.
However, I'm now replacing the default index page with a newer grid that loads via AJAX with filtering and paging mechanism. For this, I need to convert the data to my ViewModel
I want to be able to display the address in line as text. I've tried the following:
var tmp = _context.Person.Include(x => x.Address).ToList();
tmp.ForEach(x => vm.List.Add(new IndexListItem()
{
Name = x.Name,
Address = x.Address.FirstLine + " " + x.Address.SecondLine,
ID = x.ID
}));
But, when debugging, Address seems to always be null, even if there is data on the row.
I'm pretty sure I can do this on a standard foreach loop, and doing an if null check, but, I can't help but feel this is something that is quite simple and I'm over complicating it.
Is there a simpler way to return the address details I need?
As I said in a comment, you should do this in one statement:
vm.List = _context.Person
.Select(p => new IndexListItem
{
Name = p.Name,
Address = p.Address.FirstLine + " " + p.Address.SecondLine,
ID = p.ID
}).ToList();
This has two advantages:
- It's translated into SQL, so only the four required fields are pulled from the database (which makes a considerable difference with wider records).
- EF will generate SQL that takes null values into account.
If you don't want " "
as result you could do...
Address = (p.Address.FirstLine + " " + p.Address.SecondLine).Trim()
...or...
Address = p.Address.FirstLine != null ? p.Address.FirstLine + " " : "")
+ p.Address.SecondLine
But I don't think it matters much for viewing data.
这篇关于当条目为零/空时,如何处理一个对多(或零)实体的迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!