如何在 linq to Listview 中显示 2 个查询的结果?

发布于 2024-11-08 01:50:03 字数 1472 浏览 5 评论 0原文

<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" runat="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();

这是一个示例(不需要真正使用这样的写法) 我不知道如何从两个不同的查询中获取一个列表中的不同属性。 喜欢:

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

谢谢。

<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" runat="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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-11-15 01:50:03

看一下我为向您展示 Linq Union 方法而编写的这个示例。仅当两个 Lists/IEnum 具有相同类型时(在我们的例子中 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();
}

和模型

public class Model {

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

编辑

您可以“花哨”,甚至选择联合为匿名类型。例如:

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

这将为您提供一个具有属性 yyyy 和 nnnn 的 IEnumerable。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文