<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Avivo Tech Blog &#187; linq</title>
	<atom:link href="http://tech.avivo.si/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.avivo.si</link>
	<description>Solving problems</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:46:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Transact SQL paging Store Procedure &#8211; LINQ Skip Take alternative</title>
		<link>http://tech.avivo.si/2011/04/transact-sql-paging-store-procedure-linq-skip-take-alternative/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=transact-sql-paging-store-procedure-linq-skip-take-alternative</link>
		<comments>http://tech.avivo.si/2011/04/transact-sql-paging-store-procedure-linq-skip-take-alternative/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 08:03:10 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq skip take alternative]]></category>
		<category><![CDATA[ms sql server]]></category>
		<category><![CDATA[paging in t-sql]]></category>
		<category><![CDATA[store procedure]]></category>
		<category><![CDATA[transact sql]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=1307</guid>
		<description><![CDATA[This Transact-SQL Store Procedure allows you to do paging on your SQL table by providing only two parameters: Current page Record to display per page It returns chosen records and also total count as output parameter if you need to display this information or calculate how many pages you have in paging control. CREATE PROCEDURE [...]]]></description>
			<content:encoded><![CDATA[<p>This Transact-SQL Store Procedure allows you to do paging on your SQL table by providing only two parameters:</p>
<ul>
<li> Current page</li>
<li> Record to display per page</li>
</ul>
<p>It returns chosen records and also total count as output parameter if you need to display this information or calculate how many pages you have in paging control.</p>
<pre class="sql">CREATE PROCEDURE [dbo].[YourProcedureName]
  @CurrentPage int,
  @RecordsPerPage int,
  @Count int OUTPUT
AS
BEGIN
  -- The number of rows affected by the different commands
  -- does not interest the application, so turn NOCOUNT ON
  SET NOCOUNT ON

  -- Determine the first record and last record
  DECLARE @FirstRecord int, @LastRecord int

  SELECT @FirstRecord = (@CurrentPage - 1) * @RecordsPerPage
  SELECT @LastRecord = (@CurrentPage * @RecordsPerPage + 1)
  SET @Count = (SELECT COUNT(*) FROM YourTable);

  WITH TempResult as
  (
    SELECT	ROW_NUMBER() OVER(ORDER BY Field1 DESC) as RowNumber,
            Field1,
            Field2
    FROM    YourTable
  )
  SELECT  TOP (@LastRecord - 1) *
  FROM    TempResult
  WHERE   RowNumber &gt; @FirstRecord AND
          RowNumber &lt; @LastRecord

  -- Turn NOCOUNT back OFF
  SET NOCOUNT OFF
END</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2011/04/transact-sql-paging-store-procedure-linq-skip-take-alternative/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQ Max and Min functions upgraded</title>
		<link>http://tech.avivo.si/2011/04/linq-max-and-min-functions-upgraded/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-max-and-min-functions-upgraded</link>
		<comments>http://tech.avivo.si/2011/04/linq-max-and-min-functions-upgraded/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 09:00:06 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datacontext]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[firstordefault]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq min function throws an exception]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[min]]></category>
		<category><![CDATA[min linq function throws an exception]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=1266</guid>
		<description><![CDATA[Classic LINQ Max and Min functions have the same problem as First function &#8211; exception can occur, so we use FirstOrDefault which returns null instead of exception. So, this is how you can extend Min and Max functions into MinOrDefault and MaxOrDefault; public static class IEnumerableExtensions { public static int MinOrDefault(this IEnumerable source, Func selector, [...]]]></description>
			<content:encoded><![CDATA[<p>Classic LINQ <strong>Max </strong>and <strong>Min </strong>functions have the same problem as <strong>First </strong>function &#8211; exception can occur, so we use <strong>FirstOrDefault </strong>which returns <strong>null </strong>instead of exception.</p>
<p>So, this is how you can extend <strong>Min </strong>and <strong>Max </strong>functions into <strong>MinOrDefault </strong>and <strong>MaxOrDefault</strong>;</p>
<pre class="csharp">
public static class IEnumerableExtensions
{
  public static int MinOrDefault<TSource>(this IEnumerable<TSource> source,
    Func<TSource, int> selector, int defaultValue)
  {
    if (source.Any<TSource>())
      return source.Min<TSource>(selector);

    return defaultValue;
  }

  public static int MaxOrDefault<TSource>(this IEnumerable<TSource> source,
    Func<TSource, int> selector, int defaultValue)
  {
    if (source.Any<TSource>())
      return source.Max<TSource>(selector);

    return defaultValue;
  }
}
</pre>
<p>&nbsp;</p>
<p><strong>Usage example:</strong></p>
<pre class="csharp">
  YourDataContext db = new YourDataContext();
  int maxVisits = db.SomeEntityTables.MaxOrDefault(x => x.SomeFieldYouSearchForMaximum, 0);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2011/04/linq-max-and-min-functions-upgraded/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Remove items from List using LINQ</title>
		<link>http://tech.avivo.si/2010/11/remove-items-from-list-using-linq/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=remove-items-from-list-using-linq</link>
		<comments>http://tech.avivo.si/2010/11/remove-items-from-list-using-linq/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 18:13:52 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[ms sql server]]></category>
		<category><![CDATA[Remove items from list using linq]]></category>
		<category><![CDATA[remove items in runtime]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=821</guid>
		<description><![CDATA[Typical scenario: you got your list of items from database but you want to remove some items from it (items that match additional criterion). It is simple as this (i.e. assuming that your list is named yourItems): yourItems.RemoveAll(x => x.ItemPropertyId != null);]]></description>
			<content:encoded><![CDATA[<p>Typical scenario: you got your list of items from database but you want to remove some items from it (items that match additional criterion).</p>
<p>It is simple as this (i.e. assuming that your list is named <strong>yourItems</strong>):</p>
<pre class=csharp>
yourItems.RemoveAll(x => x.ItemPropertyId != null);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2010/11/remove-items-from-list-using-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random sorting using LINQ</title>
		<link>http://tech.avivo.si/2010/07/random-sorting-using-linq/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=random-sorting-using-linq</link>
		<comments>http://tech.avivo.si/2010/07/random-sorting-using-linq/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 09:07:43 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[random sorting]]></category>
		<category><![CDATA[random sorting using linq]]></category>
		<category><![CDATA[shuffle with linq]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sort randomly]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=545</guid>
		<description><![CDATA[Random Sort Consider the below Person class: public class Person { public int Id { get; set; } public string Name { get; set; } } This is how you can randomly sort the List object: List list = new List(); list.Add(new Person { Id = 1, Name = "Davolio Nancy" }); list.Add(new Person { [...]]]></description>
			<content:encoded><![CDATA[<p>Random Sort</p>
<p>Consider the below Person class:</p>
<pre class="csharp">
public class Person
{
    public int Id
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }
}
</pre>
<p>This is how you can randomly sort the List<Person> object:</p>
<pre class="csharp">
List<Person> list = new List<Person>();

list.Add(new Person { Id = 1, Name = "Davolio Nancy" });
list.Add(new Person { Id = 2, Name = "Fuller Andrew" });
list.Add(new Person { Id = 3, Name = "Leverling Janet" });
list.Add(new Person { Id = 4, Name = "Peacock Margaret" });
list.Add(new Person { Id = 5, Name = "Buchanan Steven" });
list.Add(new Person { Id = 6, Name = "Suyama Michael" });
list.Add(new Person { Id = 7, Name = "King Robert" });
list.Add(new Person { Id = 8, Name = "Callahan Laura" });
list.Add(new Person { Id = 9, Name = "Dodsworth Anne" });

list = list.OrderBy(x => Guid.NewGuid()).ToList();
</pre>
<p>Or you can use this approach:</p>
<pre class="csharp">
Random random = new Random();
list = (from x in list
       let r = random.Next()
       orderby r
       select x).ToList();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2010/07/random-sorting-using-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Linq to SQL classes from .sdf (Local database)</title>
		<link>http://tech.avivo.si/2010/03/create-linq-to-sql-classes-from-sdf-local-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-linq-to-sql-classes-from-sdf-local-database</link>
		<comments>http://tech.avivo.si/2010/03/create-linq-to-sql-classes-from-sdf-local-database/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 18:53:00 +0000</pubDate>
		<dc:creator>developer</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[code generator]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[sdf]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=462</guid>
		<description><![CDATA[Linq allows to handle data easily. Linq to SQL tools are integrated in Visual Studio but is limited to can generate code only for  certain databases. To create ORM from .sdf database file read on&#8230; What to do? Copy next script to a file with .bat extension. Change DATABASE_NAME, DATABASE_PASSWORD and NAMESPACE according to your [...]]]></description>
			<content:encoded><![CDATA[<p>Linq allows to handle data easily. Linq to SQL tools are integrated in Visual Studio but is limited to can generate code only for  certain databases. To create ORM from .sdf database file read on&#8230;</p>
<p><strong>What to do?</strong><br />
Copy next script to a file with <em>.bat</em> extension.</p>
<p>Change <em>DATABASE_NAME</em>,<em> DATABASE_PASSWORD </em>and <em>NAMESPACE </em>according to your needs.</p>
<pre class="batch">@echo off
rem Input parameters
rem DATABASE_NAME should NOT contain spaces
set DATABASE_NAME=MyDatabase
set DATABASE_PASSWORD=mypass
set NAMESPACE=MyNamespace.Database

rem Set full path to SqlMetal.exe
rem For 64-bit Windows
set SQLMETAL="%ProgramW6432%\Microsoft SDKs\Windows\v6.0A\Bin\SqlMetal.exe"
rem For 32-bit Windows
if "%ProgramW6432%"=="" set SQLMETAL="%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\Bin\SqlMetal.exe"

rem Other parameters
set DATABASE_PATH="%CD%\%DATABASE_NAME%.sdf"
set OUTPUT_CS_PATH="%CD%\%DATABASE_NAME%.cs"
set OUTPUT_DBML_PATH="%CD%\%DATABASE_NAME%.dbml"
set CONNECTION_STRING="Data Source=\"%DATABASE_PATH%\"; Password=\"%DATABASE_PASSWORD%\";"

rem Check and run sqlmetal
if not exist %SQLMETAL% goto error-missing-sqlmetal
%SQLMETAL% /conn:%CONNECTION_STRING% /dbml:%OUTPUT_DBML_PATH% /namespace:%NAMESPACE% /context:%DATABASE_NAME%DataContext /pluralize
%SQLMETAL% /conn:%CONNECTION_STRING% /code:%OUTPUT_CS_PATH% /namespace:%NAMESPACE% /context:%DATABASE_NAME%DataContext /pluralize
goto end

:error-missing-sqlmetal
echo Error: cannot find sqlmetal.exe
echo Search location: %SQLMETAL%
goto end

:end</pre>
<p><strong>Example</strong></p>
<p>If the database is stored in <em>C:\db\Website.sdf</em> then copy script into <em>C:\db\make.bat</em>. Change <em>DATABASE_NAME</em> to <em>Website </em>and run the script. Script should generate <em>C:\db\Website.cs</em> and <em>C:\db\Website.dbml</em> files. Include the generated <em>.cs</em> file into your project.</p>
<p><strong>Using the generated code</strong></p>
<pre class="c-sharp">class Program
{
  static void Main()
  {
    //Suppose that Website.sdf has Page table with CreationDate column of type DateTime
    WebsiteDataContext db = new WebsiteDataContext(@"C:\db\Website.sdf");
    var list = from p in db.Pages
                 where p.CreationDate &gt; DateTime.Today
                 selet p;

    //Show number of pages that match Linq query
    Console.WriteLine(list.Count() + " pages created today.");
  }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2010/03/create-linq-to-sql-classes-from-sdf-local-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The custom tool &#8216;MSLinqToSQLGenerator&#8217; failed. Could not retrieve the current project.</title>
		<link>http://tech.avivo.si/2009/09/the-custom-tool-mslinqtosqlgenerator-failed-could-not-retrieve-the-current-project/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-custom-tool-mslinqtosqlgenerator-failed-could-not-retrieve-the-current-project</link>
		<comments>http://tech.avivo.si/2009/09/the-custom-tool-mslinqtosqlgenerator-failed-could-not-retrieve-the-current-project/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 14:30:59 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linqtosql]]></category>
		<category><![CDATA[MSLinqToSQLGenerator failed]]></category>
		<category><![CDATA[visual studio 2008]]></category>
		<category><![CDATA[vs 2008]]></category>

		<guid isPermaLink="false">http://blog.sweetucan.com/?p=163</guid>
		<description><![CDATA[To correct this, open up the Visual Studio 2008 command prompt (or any command prompt and navigate to the location of the VS exe &#8216;devenv.exe&#8217;) and type the following command: devenv /ResetSkipPkgs Then open your LinqToSql file(s) (*.dbml) and save to trigger the code generator tool.  After that, you should hopefully be up and running [...]]]></description>
			<content:encoded><![CDATA[<p align="left"><span style="font-family: Tahoma;">To correct this, open up the Visual Studio 2008 command prompt (or any command prompt and navigate to the location of the VS exe &#8216;devenv.exe&#8217;) and type the following command:</span></p>
<p align="left"><span style="font-family: Courier;">devenv /ResetSkipPkgs</span></p>
<p align="left"><span style="font-family: Tahoma;">Then open your LinqToSql file(s) (*.dbml) and save to trigger the code generator tool.  After that, you should hopefully be up and running once more.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2009/09/the-custom-tool-mslinqtosqlgenerator-failed-could-not-retrieve-the-current-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

