0

我正在将一个选中的列表框与以下 ling 绑定,以重新运行列表列表,但如果列表为空,我应该如何管理它?它在 ling 语句中引发异常,如果为 null,我应该为复选框设置什么

public List<dataObject> GetAllCustomItems(CategoryType currType, int mCategoryID)
        {
            List<dataObject> lst = null;
            try
            {
                switch (currType)
                {
                    case CategoryType.Dressing:
                        lst = (from xx in this.DressingItems 
                               where xx.DressingInfo.CatID == mCategoryID 
                               select new dataObject() { 
                                   ID = xx.DressingInfo.DressingID,
                                   Name = xx.DressingInfo.Description, 
                                   Selected = xx.IsDefault 
                               }).ToList();
                        break;
              }
        }

它抛出异常

Value cannot be null.
Parameter name: source
4

4 回答 4

1

我不认为是 xx.DressingInfo 对象问题,因为它会首先出现未找到对象引用,肯定是来自 xx.DressingInfo 的值有问题。你可以试试iif检查一下。

  var lst = (from xx in this.DressingItems 
                           where xx.DressingInfo.CatID == mCategoryID 
                           select new dataObject() { 
                               ID = xx.DressingInfo.DressingID==null?0:xx.DressingInfo.DressingID,
                               Name = xx.DressingInfo.Description==null? "":xx.DressingInfo.Description, 
                               Selected = xx.IsDefault 
                           });
return IsNullThenNew<dataObject>(lst);   

如果您不想将 null 返回给调用者,您可以通过下面的通用方法返回至少一个空对象。

 public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
        {
            return enumerable == null || !enumerable.Any();
        }
 public static List<T> IsNullThenNew<T>(this IEnumerable<T> t)
        {
            if (!IsNullOrEmpty<T>(t))
            {
                return t.ToList();
            }
            else
            {
                Type genericListType = typeof(List<>);
                Type listType = genericListType.MakeGenericType(t.GetType());
                object listInstance = Activator.CreateInstance(listType);
                return (List<T>)listInstance;
            }//end if-else
        }
于 2013-08-02T08:35:46.053 回答
1

你确定this.DressingItems不为空或为空吗?

在您的 LINQ 查询之前检查:

if (this.DressingItems != null && this.DressingItems.Any()) {
    lst = (from xx in this.DressingItems where...
}

lst如果DressingItems为 null 或为空,则返回 null。

编辑:(在操作评论之后)

另外,检查 DressingInfo 属性:

where xx.DressingInfo != null &&...
于 2013-08-02T08:16:32.963 回答
1

在 where 子句中首先检查 DressingInfo 是否为 null 怎么样

where xx.DressingInfo != null && xx.DressingInfo.CatID == mCategoryID
于 2013-08-02T08:17:14.737 回答
0

您可以更改List<dataObject> lst = null;List<dataObject> lst = new List<dataObject>; 或者做这样的事情:

public List<dataObject> GetAllCustomItems(CategoryType currType, int mCategoryID)
{
    try
    {
        switch (currType)
        {
            case CategoryType.Dressing:
                List<dataObject> lst = (from xx in this.DressingItems 
                       where xx.DressingInfo.CatID == mCategoryID 
                       select new dataObject() { 
                           ID = xx.DressingInfo.DressingID,
                           Name = xx.DressingInfo.Description, 
                           Selected = xx.IsDefault 
                       }).ToList();
                break;
      }
}
于 2013-08-02T08:16:19.013 回答