简体   繁体   中英

LINQ to SQL Default Value ?? nullable

Problem: How to get Linq to make a default value of 'noImage.jpg' if null is returned from the database.

I'm querying a single table:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="Materials.MaterialsDataClassesDataContext"
    TableName="Materials" EnableDelete="True" EnableInsert="True" EnableUpdate="True">
</asp:LinqDataSource>

then displaying images:

<telerik:GridTemplateColumn HeaderText="Image" UniqueName="Image">  
                        <ItemTemplate>  
                            <a href="<%=VirtualPathUtility.ToAbsolute("~/")%>showFrontEndMaterialDetail.aspx?materialId=<%# Eval("Id")%>">
                            <img src="<%=VirtualPathUtility.ToAbsolute("~/")%>Images/Uploaded/Thumbs/<%# Eval("Image1") %>"></img></a>
                        </ItemTemplate>  
                     </telerik:GridTemplateColumn>

I thought I had it (but didn't) with:

<%# (string?)Eval("Image1") ?? "noImage.jpg" %>

怎么样

String.IsNullOrEmpty(Convert.ToString(Eval("Image1"))) ? "noImage.jpg" : Eval("Image1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM