2

In my asp.net application I am creating a custom user control (which is similar to GridView). It takes list of an object (like Car, Person etc..) and shows them as table.

Since list of objects can be different type when I try to cast from a custom list to list of objects it gives me an error like: "Unable to cast object of type 'System.Collections.Generic.List1[Main.People]' to type 'System.Collections.Generic.List1[System.Object]'."

Is there any better way to handle this?

Code is like:

 public List<object> Objects = new List<object>();
    protected void Page_Load(object sender, EventArgs e)
    {
        Objects = (List<object>) HttpContext.Current.Items["Objects"];
    }

<table>
<% int counter = 0; %>
<% foreach (object customObject in Objects)
   { %>
<% counter++; %>
<%PropertyInfo[] propertyInfos = customObject.GetType().GetProperties(); %>
<%--header--%>
<% if (counter == 1)
   {
%>
<tr>
    <%
       foreach (var reportField in propertyInfos)
       {
    %>
    <th>
        <%=reportField.Name %>
    </th>
    <%
           }
    %>
</tr>
<%
   } %>
<%--data rows--%>
<tr>
    <%
       foreach (var reportField in propertyInfos)
       {
    %>
    <td>
        <a href="?page=Details.aspx?<%=reportField.Name %>=<%=reportField.GetValue(customObject, null) %>">
            <%=reportField.GetValue(customObject, null)%>
        </a>
    </td>
    <%
           }
    %>
</tr>
<% }%>
</table>

Short-circuit UNION? (only execute 2nd clause if 1st clause has no results)

If I do:

SELECT * FROM A   
WHERE conditions  
UNION  
SELECT * FROM B   
WHERE conditions 

I get the union of the resultset of query of A and resultset of query of B.

Is there a way/operator so that I can get a short-circuit OR result instead?

I.e. Get the result of SELECT * FROM A WHERE conditions and only if this returns nothing get the resultset of the SELECT * FROM B WHERE conditions ?

4

3 回答 3

2

您应该能够在 Linq 中使用 Cast() 扩展方法

Objects = HttpContext.Current.Items["Objects"].Cast<object>().ToList()

http://msdn.microsoft.com/en-us/library/bb341406.aspx

于 2013-09-03T22:28:02.263 回答
1

由于看起来您实际上并没有修改代码中的列表,因此您实际上并不需要它是一个列表。它可以只是一个IEnumerable<object>

public IEnumerable<object> Objects;
protected void Page_Load(object sender, EventArgs e)
{
    Objects = (IEnumerable<object>)HttpContext.Current.Items["Objects"];
}
于 2013-09-03T22:29:01.160 回答
0

让你的所有类:汽车,人从基类MyBaseClass继承并将你的列表转换为该类型/类

public List<MyBaseClass> Objects = new List<MyBaseClass>();

protected void Page_Load(object sender, EventArgs e)
{
    Objects = (List<MyBaseClass>) HttpContext.Current.Items["Objects"];
}

然后你可以迭代你的 List 并确定确切的类型

foreach(MyBaseClass mbc in Objects)
{
    if(mbc is People)
    {
        //its a People object
    }
    else if(mbc is Car)
    {
        //ist a Car object
    }
}

请记住,在您的代码中,当您添加或创建对象列表时,您需要将其声明为

List<MyBaseClass> Objects 

顺便说一句,您当前的列表似乎是人员列表,所以这应该可以

public List<People> Objects = new List<People>();

protected void Page_Load(object sender, EventArgs e)
{
    Objects = (List<People>) HttpContext.Current.Items["Objects"];
}
于 2013-09-03T22:30:12.193 回答