如何在 LINQ 查询中添加换行符?

发布于 2024-11-02 16:01:18 字数 963 浏览 0 评论 0原文

我需要在“选择新”部分的 LINQ 查询中添加换行符。我尝试了
"\n" 但这些似乎不起作用。知道使用哪个命令来获得换行符吗?

我想使用 linq 在航空公司符号下方显示航空公司图像。我应该将图像路径存储在xml文件中然后使用linq查询吗?这是我到目前为止得到的代码:

var query = from f in XElement.Load(MapPath("flightdata2.xml")).Elements("flight") 选择新的 {
航空公司 = (string)f.Element("航空公司"), DepartureAirportSymbol = (string)f.Element("departureAirportSymbol"), 出发时间 = (string)f.Element("出发时间"), DestinationAirportSymbol = (string)f.Element("destinationAirportSymbol"), 到达时间 = (string)f.Element("到达时间"), Stops = (int)f.Element("numberOfStops"), 持续时间=(字符串)f.Element(“持续时间”), 小屋 = (string)f.Element("class"), 价格 = "$" + (Int32)f.Element("价格") };

I need to add a line break in my LINQ query in the "select new" section. I tried <br /> and "\n" but those didn't seem to work. Any clue which command to use to get the line break?

I want to display airline image underneath the airline symbol using linq. Should I store the image path in the xml file and then query using linq? Here's the code I've got so far:


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")
};

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

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

发布评论

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

评论(3

雪化雨蝶 2024-11-09 16:01:18

尝试

 Environment.NewLine  

对于大多数情况,它与 \r\n 相同,但最好是在不使用 \r\n 的系统上运行。

Try

 Environment.NewLine  

For most cases it's the same as \r\n, but it's better in case it ever got run on a system that didn't use \r\n.

如梦初醒的夏天 2024-11-09 16:01:18

System.Environment.NewLine 对于在典型设置中的字符串中插入换行符非常有用。但是,如果您尝试在网页中产生换行符,则需要使用 "
"
作为 HTML 换行符。

System.Environment.NewLine is useful for inserting line breaks in strings in a typical setting. However, if you're trying to produce a line break in a web page, then you need to use "<br />" for an HTML line break.

执着的年纪 2024-11-09 16:01:18

如果我猜的话,我会说您可能想使用 String.Join(@"
", results); 展平查询结果以进行显示如果是这样,我会探索使用绑定控件。

但我们只能猜测,完全没有任何信息可以继续......

现在到底正在发生什么。您使用什么代码?您希望看到什么发生?

编辑:从评论中提取此内容以回应您的澄清:

我会在网格视图中使用模板字段,或者使用转发器而不是网格视图。这是一个演示问题,而不是数据问题。不必担心尝试在 LINQ 中执行此操作。

模板字段或重复器将允许您将两个“字段”放置在同一行的彼此之上以用于显示目的:

http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs< /a>

If I were to guess, I'd say you probably want to flatten the results of a query for display using String.Join(@"<br />", results); If so, I would explore using a bound control instead.

But then we can only guess having absolutely no information to go on...

What, exactly, is happening now. What code are you using? What would you like to see happen?

Edit: Pulling this up from comments in response to your clarification:

I would use a template field in your gridview, or use a repeater instead of a gridview. This is a presentation concern, not a data concern. Don't worry about trying to do it in LINQ.

Either a template field or a repeater will allow you to place two "fields" on top of each other on the same row for display purposes:

http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs

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