使用LINQ查询显示图像

时间:2011-04-20 19:13:57

标签: c# asp.net linq

我想在航空公司名称下面显示航空公司图片。我已在我的XML文件中为airlineImage存储了路径。我如何使用LINQ在我的查询中显示图像?

到目前为止,这是我的代码:

var query = from f in XElement.Load(MapPath("flightdata2.xml")).Elements("flight")
                    select new
                    {                        
                        Airline = (string)f.Element("airline"),
                        DepartureAirportSymbol = (string)f.Element("departureAirportSymbol"),
                        DepartTime = (string)f.Element("departureTime"),
                        DestinationAirportSymbol = (string)f.Element("destinationAirportSymbol"),
                        ArrivalTime = (string)f.Element("arrivalTime"),
                        Stops = (int)f.Element("numberOfStops"),
                        Duration = (string)f.Element("duration"),
                        Cabin = (string)f.Element("class"),
                        Price = "$" + (Int32)f.Element("price")
                    };

    this.GridView1.DataSource = query;
    this.GridView1.DataBind();

2 个答案:

答案 0 :(得分:1)

我认为最好通过将路径加载到对象中然后动态设置图像的src属性来实现最佳效果:

<img src='<%# Eval("ImageURL") %>' />

答案 1 :(得分:1)

试试这个:

select new
{
   Duration = (string)f.Element("duration"),
   Cabin = (string)f.Element("class"),
   Price = "$" + (Int32)f.Element("price")
   ImagePath = (string)f.Element("airlineImage")
};


<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
   <Columns>
      <asp:ImageField DataImageUrlField="ImagePath" 
                      DataImageUrlFormatString="~/Content/Images/{0}" />
   </Columns>
</asp:GridView>

UPDATE 1.您必须将GridView中的路径~/Content/Images/{0}替换为存储图像的位置。 2.设置AutoGenerateColumns =“false”,这样您的网格将只包含您定义的列。