开发者

How can I show results from 2 query in linq to Listview?

开发者 https://www.devze.com 2023-03-06 09:54 出处:网络
<asp:ListView ID=\"ListView1\" runat=\"server\" GroupItemCount=\"5\"> <LayoutTemplate> <table runat=\"server\" id=\"table1\">
<asp:ListView ID="ListView1" runat="server" GroupItemCount="5">
    <LayoutTemplate>
        <table runat="server" id="table1">
            <tr runat="server" id="groupPlaceholder">
            </tr>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr runat="server" id="tableRow">
            <td runat="server" id="itemPlaceholder" />
        </tr>
    </GroupTemplate>
    <ItemTemplate>
        <td id="Td1" runat="server">
            <%-- Data-bound content. --%>
            <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("yyyy") %>' />
        </td>
         <td id="Td2" runat="server">
            <%-- Data-bound content. --%>
            <asp:Label ID="NameLabel" ru开发者_运维问答nat="server" Text='<%#Eval("nnnn") %>' />
        </td>
    </ItemTemplate>
</asp:ListView>
var by1 = from x in model.x
          xxxx
          select new
          {
              x.yyyy
          };
ListView1.DataSource = by1;
ListView1.DataBind();

var by2 = from z in model.z
          zzzz
          select new
          {
              z.nnnn
          };
ListView1.DataSource = by2;
ListView1.DataBind();

this is a sample(not need to realy work with write like this) I don't know how can i get in one list diffrent properties from 2 diffrent querys. like:

zzzzz:1111
zzzzz:2222
nnnn:ffff
nnnn:gggg
zzzz:3333

thanks.


Have a look at this example I whipped up to show you about the Linq Union method. This only works when both Lists/IEnum's are of the same type (in our case Model).

void Main()
{
List<Model> by1 = new List<Model>();

by1.Add(new Model {Name = "zzzz",Value = "1111" });
by1.Add(new Model {Name = "fdgdfg",Value = "1234"});
by1.Add(new Model {Name = "zzzz",Value = "2222"});

List<Model> by2 = new List<Model>();
by1.Add(new Model {Name = "nnnn",Value = "ffff"});
by1.Add(new Model {Name = "nnnn",Value = "gggg"});
by1.Add(new Model {Name = "zzzz",Value = "3333"});

// Join the results of by1 and by2 as both are List<Model>

object dataSource = by1.Union(by2); // Results in a IEnumerable<Model>

// Bind the dataSource to the ListView

ListView1.DataSource = dataSource;
ListView1.DataBind();
}

And the Model

public class Model {

public string Name { get;set;}
public string Value { get;set;}
}

Edit

You could be 'fancy' and even select the union into an anonymous type. For example:

    object dataSource = by1.Union(by2).Select(item=>new
                            {
                                 yyyy = item.Name,
                                 nnnn = item.Value
                            });

Which will give you an IEnumerable with properties yyyy and nnnn.

0

精彩评论

暂无评论...
验证码 换一张
取 消