score:6
as a quick win, you can remove the .tolist()
call. all you're doing is enumerate items, so there's no need to do that. similarly, there's no need to create bookandautherlist.
eventually i think you can strip it down to this:
public ienumerable<book> getauthorwithbookname(string keyword)
{
return from author in getauthorname(keyword)
let book = getbook(author)
from xmlbook in xdocument.parse(book.outerxml).descendants("books")
select new book
{
authorname = author.authorname,
bookname = xmlbook.attribute("name").value
};
}
score:0
try this:
foreach (var bookname in booknamelist)
{
yield return new book()
{
authorname = author.authorname,
bookname = bookname
};
}
and remove other return
statement.
score:1
not sure you'll get much 'optimization'. yield is better used when you would often break part way through using the enumeration you are generating, so that you don't do unnecessary computation. but here goes:
this is untested.
public ienumerable<book> getauthorwithbookname(string keyword)
{
foreach (var author in getauthorname(keyword))
{
xdocument xdocbooksnames = xdocument.parse(getbook(author).outerxml);
var booknamelist = from x1 in xdocbooksnames.descendants("books")
select x1.elements("book").select(g => g.attribute("name").value);
foreach (var bookname in booknamelist)
{
yield return new book()
{
authorname = author.authorname,
bookname = bookname
};
}
}
}
what i did:
- remove the tolist() on the expression, it already returns an enumerable. removed some code by consolidating getauthorname into the foreach (note - make sure that function yields and ienumerable too, if you can)
- yield return instead of adding to a list
score:9
responses by rubens and luke correctly explain the use of yield.
however, this looks suspicious to me.
xmlnode booksnames = getbook(author);
xdocument xdocbooksnames = xdocument.parse(booksnames.outerxml);
you convert xml to string and then parse it again, only because you want to convert it from dom node to xml.linq node. if you are talking about optimization, then this is much more inefficient than creating an extra list.
Source: stackoverflow.com
Related Query
- Optimize Code by using yield or changing algorithm
- How to optimize a code using DataTable and Linq?
- Convert string[] to int[] in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Entity-framework code is slow when using Include() many times
- MasterMind scoring algorithm in C# using LINQ
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- can my code improve from using LINQ?
- LINQ Source Code Available
- Solution to travelling salesman problem using nearest neighbour algorithm in one LINQ query?
- .NET 4 Code Contracts: "requires unproven: source != null"
- How can I write the following code more elegantly using LINQ query syntax?
- What about using LINQ or Yield Return in for loops?
- How can I code an outer join using LINQ and EF6?
- Explanation of Code : retrieve item from Array using FirstorDefault()
- Changing a piece of code into expressions
- C# .Net 3.5 Code to replace a file extension using LINQ
- Lexicographical sort algorithm using C#
- Entity Framework Code First using context in Controller
- Trying to understand LINQ code using c#
- C# - Linq optimize code with List and Where clause
- Retrieve bool result by using LinQ code
- Unit testing code using IQueryable
- How can I combine IObservable<T>.Throttle() with some other event source using Reactive Extensions?
- creating Linq to sqlite dbml from DbLinq source code
More Query from same tag
- Linq select records that match a list of IDs
- Combining Variable Numbers of Lists w/ LINQ
- Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List
- Split a collection into parts based on condition with LINQ?
- Linq IList<T> hide data and bind to gridview
- Complex Linq query
- How to use condition for all children in Entity Framework
- C# query with dynamic tablename
- Can I use a post-conditional statement with LINQ?
- Linq query definition
- Using SQL Server Filestream with LINQ
- How to perform a this kind of Distinct operation with LINQ?
- Why is my query so slow?
- Difference between “Equals” and “SequenceEqual”?
- In IEnumerable extensions - why is only Count() optimized for ICollection?
- StackOverflowException using Linq(Kit) for nested data
- Select the Object with linq that best fit the Query strings
- Linq and IEnumerable lazy query. Efficiency lost?
- Write sql query to linq
- Left Outer Join with Multiple On Statements
- How to query in linq and check if the passed value is empty
- GroupBy on list property → object in each group
- What is an easy way to append or prepend a single value to an IEnumerable<T>?
- Group data on a single column and merge another column
- IEnumerable<List<DataRow>> does not contain a definition for ToList()
- How to generate generic GetById(int Id) function in Linqtosql?
- Full join in Linq
- Why is this LINQ to EF query not working?
- How can find specific HashSet elements with linq
- Strategies for replacement of systemic objects