ashley2009
asked on
How to select multiple objects in a LINQ Query?
How to select multiple objects in a LINQ Query? I am working with Yahoo politics news feed. I want to display only news that has "Obama" name in description. The idea is I want to only fetch elements and populate the "var" if certain key words exist in the description element, but I cannot do it; however, I am trying, but cannot generate the solution. My query looks like this, which is definitely not producing result:
var feeds = from feed in xdoc.Descendants("item")
where feed.Element("description" ).Value == "Obama"
select new
{
title = feed.Element("title").Valu e,
url = feed.Element("link").Value .Replace(r eplaceStri ng, ""),
description = feed.Element("description" ).Value.
Replace(replaceString, "").Replace("&", "&").Replace("&mda sh;", "--").Contains("Obama"),
date = feed.Element("pubDate").Va lue
};
Please tell me how to select multiple objects in a LINQ query. I also paste the front end and back end files.
var feeds = from feed in xdoc.Descendants("item")
where feed.Element("description"
select new
{
title = feed.Element("title").Valu
url = feed.Element("link").Value
description = feed.Element("description"
Replace(replaceString, "").Replace("&", "&").Replace("&mda
date = feed.Element("pubDate").Va
};
Please tell me how to select multiple objects in a LINQ query. I also paste the front end and back end files.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Your Daily Source of News By LINQ .NET 3.5</title>
<style type="text/css">
body {
font-family:Verdana;
color:#CCCCFF;
background-color:Black;
}
.accordionHeader
{
border: 1px solid #EBDDE2;
color: white;
background-color: #5E5A80;
font-family: Verdana;
font-size: 12px;
font-weight: bold;
padding: 5px;
margin-top: 5px;
cursor: pointer;
}
.accordionContent
{
border: 2px solid #EBDDE2;
color: #ccccff;
font-family: Verdana;
font-size: 12px;
padding: 5px;
margin-top: 5px;
}
.accordionIntroContent
{
padding: 1px;
}
.accordionIntroContent a:link
{
color: #5E5A80;
font-family: Verdana;
font-size: 10px;
text-decoration:none;
}
.accordionIntroContent a:hover
{
color: #5E5A80;
font-family: Verdana;
font-size: 10px;
text-decoration:underline;
}
.accordionIntroContent a:visited
{
color: #5E5A80;
font-family: Verdana;
font-size: 10px;
text-decoration:none;
}
#page-wrap {
background: white;
min-width: 500px;
max-width: 1260px;
margin: 10px auto;
}
#page-wrap #inside {
margin: 30px 30px 0px 30px;
padding-top: 10px;
padding-bottom: 5px;
}
#leftContent
{
float: left;
padding-left: 50px;
padding-top: 20px;
color:#444444;
font-family:Verdana;
font-size:12px;
}
#contentHeader
{
font-stretch:wider;
}
.contet
{
border-left-color:#444;
border-left-style:dashed;
border-left-width:1px;
height:400px;
overflow:auto;
background-color:Black;
}
#content {
padding-left: 230px;
padding-right:100px;
}
#header {
text-align: center;
padding-bottom:10px;
color:#5E5A80; font-family:Verdana; letter-spacing:.1em;
}
.button
{
background-color:Transparent;
font-family:Verdana;
font-size:10px;height:20px;
text-align:center;
border-style:outset;
border-bottom-color:#FEFFF1;
border-width:2;
}
#contentHeader a:link
{
color:#5E5A80;
text-decoration:none;
letter-spacing:.2em;font-size:15px;font-weight:bold;
}
#contentHeader a:hover
{
color:#5E5A80;
text-decoration:underline; letter-spacing:.2em;font-size:15px;font-weight:bold;
}
#contentHeader a:visited
{
color:#5E5A80;
text-decoration:none;
letter-spacing:.2em;font-size:15px;font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="page-wrap">
<div id="inside"><div id="header">
<asp:HyperLink ID="HyperLink2" runat="server"><h1>Your Daily Source of News by Linq .NET 3.5</h1></asp:HyperLink>
</div>
<div id="leftContent">
<table><tr><td>Select a category:</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" runat="server" Font-Names="Verdana" Font-Size="8" ForeColor="#444444">
<asp:ListItem Value="topstories">Top Stories</asp:ListItem>
<asp:ListItem Value="us">US News</asp:ListItem>
<asp:ListItem Value="world">World News</asp:ListItem>
<asp:ListItem Value="politics">Politics</asp:ListItem>
<asp:ListItem Value="business">Business</asp:ListItem>
<asp:ListItem Value="tech">Technology</asp:ListItem>
<asp:ListItem Value="mostviewed">Most Viewed</asp:ListItem>
<asp:ListItem Value="science">Science News</asp:ListItem>
<asp:ListItem Value="health">Health News</asp:ListItem>
<asp:ListItem Value="entertainment">Entertainment</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr><td> <asp:Button ID="Button1" runat="server" Text="Submit Query" onclick="Button1_Click" CssClass="button"/>
</td></tr>
</table>
<asp:Panel ID="Panel1" runat="server" Visible="false">
<table><tr><td>Democrat</td>
<td><asp:CheckBox ID="CheckBox1" runat="server" /></td>
</tr>
<tr><td>Republican</td>
<td><asp:CheckBox ID="CheckBox2" runat="server" /></td>
</tr>
<tr><td>Unemployment</td>
<td><asp:CheckBox ID="CheckBox3" runat="server" /></td>
</tr>
<tr><td>President</td>
<td><asp:CheckBox ID="CheckBox4" runat="server" /></td>
</tr>
<tr><td>Senator</td>
<td><asp:CheckBox ID="CheckBox5" runat="server" /></td>
</tr>
<tr><td>California</td>
<td><asp:CheckBox ID="CheckBox6" runat="server" /></td>
</tr>
<tr><td colspan="2"><asp:Button ID="Button2" runat="server" Text="Button"
CssClass="button" onclick="Button2_Click" />
</td>
</tr></table>
</asp:Panel>
</div>
<div id="content">
<div id="contentHeader">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://rss.news.yahoo.com/rss/topstories">
<asp:Label ID="Label1" runat="server" Text="Yahoo! Top Stories"></asp:Label></asp:HyperLink>
</div>
<div class="contet">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="Accordion1" runat="server"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeader"
ContentCssClass="accordionContent"
AutoSize="None"
FadeTransitions="true"
TransitionDuration="250"
FramesPerSecond="30"
RequireOpenedPane="false"
SuppressHeaderPostbacks="True"
SelectedIndex="0"
>
<HeaderTemplate >
<%# Eval("title") %>
</HeaderTemplate>
<ContentTemplate> <div class="accordionIntroContent"><a href='<%#Eval("url")%>'><%#Eval("date")%></a></div>
<%#Eval("description")%>
</ContentTemplate>
</cc1:Accordion>
</div>
<br /><br />
</div>
</div>
</div>
</form>
</body>
</html>
........................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
string replaceString = "http://us.rd.yahoo.com/dailynews/rss/topstories/*";
protected void Page_Load(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load("http://rss.news.yahoo.com/rss/topstories");
var mainTitle = xdoc.Descendants("title");
var mainLink = xdoc.Descendants("link");
var feeds = from feed in xdoc.Descendants("item")
select new
{
title = feed.Element("title").Value,
url = feed.Element("link").Value.Replace(replaceString, ""),
description = feed.Element("description").Value.
Replace(replaceString, "").Replace("&", "&").Replace("&mdash;", "--"),
date = feed.Element("pubDate").Value
};
Accordion1.DataSource = feeds;
Accordion1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string selectedValue = DropDownList1.SelectedValue;
goThroughIfProcedure(selectedValue, replaceString);
string url = "http://rss.news.yahoo.com/rss/";
url += selectedValue;
XDocument xdoc = XDocument.Load(url);
var feeds = from feed in xdoc.Descendants("item")
select new
{
title = feed.Element("title").Value,
url = feed.Element("link").Value.Replace(replaceString, ""),
description = feed.Element("description").Value.
Replace(replaceString, "").Replace("&", "&").Replace("&mdash;", "--"),
date = feed.Element("pubDate").Value
};
Accordion1.DataSource = feeds;
Accordion1.DataBind();
Accordion1.SelectedIndex = 0;
}
private void goThroughIfProcedure(string selectedValue, string replaceString)
{
if (selectedValue == "topstories")
{
Label1.Text = "Yahoo! Top Stories";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/topstories";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/topstories/*";
}
else if (selectedValue == "us")
{
Label1.Text = "Yahoo! US News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/us";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/us/*";
}
else if (selectedValue == "world")
{
Label1.Text = "Yahoo! World News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/world";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/world/*";
}
else if (selectedValue == "politics")
{
Label1.Text = "Yahoo! Politics News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/politics";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/politics/*";
Panel1.Visible = true;
}
else if (selectedValue == "business")
{
Label1.Text = "Yahoo! Business news";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/business";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/business/*";
}
else if (selectedValue == "tech")
{
Label1.Text = "Yahoo! Technology News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/tech";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/tech/*";
}
else if (selectedValue == "mostviewed")
{
Label1.Text = "Yahoo! Most Viewed";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/mostviewed";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/mostviewed/*";
}
else if (selectedValue == "science")
{
Label1.Text = "Yahoo! Science News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/science";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/science/*";
}
else if (selectedValue == "health")
{
Label1.Text = "Yahoo! Health News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/health";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/health/*";
}
else if (selectedValue == "entertainment")
{
Label1.Text = "Yahoo! Entertainment News";
HyperLink1.NavigateUrl = "http://rss.news.yahoo.com/rss/entertainment";
replaceString = "http://us.rd.yahoo.com/dailynews/rss/entertainment/*";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string selectedValue = DropDownList1.SelectedValue;
goThroughIfProcedure(selectedValue, replaceString);
string url = "http://rss.news.yahoo.com/rss/";
url += selectedValue;
XDocument xdoc = XDocument.Load(url);
var feeds = from feed in xdoc.Descendants("item")
where feed.Element("description").Value == "Obama"
select new
{
title = feed.Element("title").Value,
url = feed.Element("link").Value.Replace(replaceString, ""),
description = feed.Element("description").Value.
Replace(replaceString, "").Replace("&", "&").Replace("&mdash;", "--").Contains("Obama"),
date = feed.Element("pubDate").Value
};
Accordion1.DataSource = feeds;
Accordion1.DataBind();
Accordion1.SelectedIndex = 0;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello,
thank you and your solution is working. I am sorry for the extra .Contains()
Thank you again.
thank you and your solution is working. I am sorry for the extra .Contains()
Thank you again.
NP. Glad to help :)
Open in new window