by Viper
9. June 2009 04:58
While developing Twitter applications, one of the common patterns that used to emerge is that some particular users will post lot of messages in a short time span. And when searching records, I used to end up with multiple messages from one particular user. So for some user interface, I always had to group messages by user posting the message.
Following code shows how easy it is to group records using LINQ. Just to explain the code a little bit, when search is performed for a certain query term using twitter rest api with Tweetsharp, results are returned as list of TwitterSearchStatus objects. The user who posted the message is indicated by FromScreenName property. So this property becomes the key on which grouping is to be performed. Following group statement shows how grouping is done in LINQ statement.
var resultsGrpByUser = from searchResult in searchResults
group searchResult by searchResult.FromUserScreenName
into userMessages
select new { FromUser = userMessages.Key, Messages = userMessages };
Above code uses group statement to create group on user name, and the grouping results
are returned as Dictionary of anonymous objects that have properties named FromUser and Messages. Following code snippet shows the whole implementation of grouping using LINQ.
static Dictionary<string, List<TwitterSearchStatus>> GetPopularTweets()
{
List<TwitterSearchTrend> trends = GetDailyTrends();
if (null == trends ||
trends.Count == 0)
{
return null;
}
// For now only process first trend term.
List<TwitterSearchStatus> searchResults = SearchForTerm(trends[0].Query);
if (searchResults.Count == 0)
{
return null;
}
var resultsGrpByUser = from searchResult in searchResults
group searchResult by searchResult.FromUserScreenName
into userMessages
select new { FromUser = userMessages.Key, Messages = userMessages };
Dictionary<string, List<TwitterSearchStatus>> dict =
new Dictionary<string, List<TwitterSearchStatus>>();
foreach(var userMessage in resultsGrpByUser)
{
Console.WriteLine(userMessage.FromUser);
foreach(var twitterStatus in userMessage.Messages)
{
Console.WriteLine("\t{0}", twitterStatus.Text);
}
dict[userMessage.FromUser] = userMessage.Messages.ToList();
Console.WriteLine("*************");
}
return dict;
}
|
|
|
057e266e-87db-432f-ae06-c1c485299377|0|.0
Views: 2252
Tags: linq
.Net | C# | LINQ
by Viper
2. June 2009 03:25
While working on Local Twitter application, I had to parse a very simple XML stream to create a business object. In classic XML days, we all used to do it using XPath or SAX or other related techniques. Xml to Linq and Linq to Xml provides a very intuitive way to perform parsing task. Lets look at the code snippet below.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
</Response>
private SiteUser ProcessResponse(string strResp)
{
StringReader sr = new StringReader(strResp);
XElement respElement = XElement.Load(sr);
string callStatus = (string)respElement.Element("Status");
if (string.Compare(callStatus, "OK", true) != 0)
{
return null;
}
SiteUser user = new SiteUser() {IP = (string) respElement.Element("Ip"),
City = (string) respElement.Element("City"),
Country = (string)respElement.Element("CountryName"),
CountryCode = (string)respElement.Element("CountryCode"),
RegionCode = (string)respElement.Element("RegionCode"),
RegionName = (string)respElement.Element("RegionName"),
PostalCode = (string)respElement.Element("ZipPostalCode"),
Latitude = (decimal)respElement.Element("Latitude"),
Longitude = (decimal)respElement.Element("Longitude")};
return user;
}
You can see from the code above, how easy the task is. You load up the XML string as StringReader and then use that to initialize XElement object. When you create XElement it is already at documentElement level. The rest of the API is self explanatory. You simple use Element method to access elements by their node names under a given XElement object.
|
|
|
c0b8cf99-2980-48fa-9dda-4fbfa8cd3fd9|0|.0
Views: 6174
Tags: linq, xml
LINQ | XML