none
Correct way to add a custom property? RRS feed

  • Question

  • Code Snippet


    namespace AdventureWorksDataContextTest
    {
    partial class Production_Product
    {
    public string SpecialProperty { get { return Name + ", " + ProductNumber; } }
    }

    /// <summary>
    /// Some tests for "how smart LINQ to SQL is?" interests me.
    /// </summary>
    [TestClass]
    public class AdventureWorksDataContextTest
    {
    public TestContext TestContext { get; set; }

    /// <summary>
    /// This test shows that expressions can dyamically reflect string concating operation.
    /// </summary>
    [TestMethod]
    public void SelectConcatStringTest()
    {
    AdventureWorksDataContext data = new AdventureWorksDataContext(@"Data Source=.;Initial Catalog=AdventureWorks2008;Integrated Security=True;MultipleActiveResultSets=True");
    var r = data.Production_Product.Where(p => p.Name == "Fork End").Select(p => p.Name + ", " + p.ProductNumber).FirstOrDefault();
    Assert.Inconclusive(r);

    /// exec sp_executesql N'SELECT TOP (1) [t1].[value]
    /// FROM (
    /// SELECT ([t0].[Name] + @p0) + [t0].[ProductNumber] AS [value], [t0].[Name]
    /// FROM [Production].[Product] AS [t0]
    /// ) AS [t1]
    /// WHERE [t1].[Name] = @p1',N'@p0 nvarchar(2),@p1 nvarchar(8)',@p0=N', ',@p1=N'Fork End'
    }
    [TestMethod]
    public void SelectSpecialPropertyTest()
    {
    AdventureWorksDataContext data = new AdventureWorksDataContext(@"Data Source=.;Initial Catalog=AdventureWorks2008;Integrated Security=True;MultipleActiveResultSets=True");
    var r = data.Production_Product.Where(p => p.Name == "Fork End").Select(p => p.SpecialProperty).FirstOrDefault();
    Assert.Inconclusive(r);

    /// exec sp_executesql N'SELECT TOP (1) [t0].[ProductID], [t0].[Name], [t0].[ProductNumber], [t0].[MakeFlag], [t0].[FinishedGoodsFlag], [t0].[Color], [t0].[SafetyStockLevel], [t0].[ReorderPoint], [t0].[StandardCost], [t0].[ListPrice], [t0].[Size], [t0].[SizeUnitMeasureCode], [t0].[WeightUnitMeasureCode], [t0].[Weight], [t0].[DaysToManufacture], [t0].[ProductLine], [t0].[Class], [t0].[Style], [t0].[ProductSubcategoryID], [t0].[SellStartDate], [t0].[SellEndDate], [t0].[DiscontinuedDate], [t0].[rowguid] AS [Rowguid], [t0].[ModifiedDate]
    /// FROM [Production].[Product] AS [t0]
    /// WHERE [t0].[Name] = @p0',N'@p0 nvarchar(8)',@p0=N'Fork End'
    }
    }
    }


    I used the AdventureWorks database for this test, and how to write the "SpecialProperty" in the partial definition of class Production_Product, and make the SelectSpecialPropertyTest generats same query as SelectConcatStringTest?

    Thanks~
    Tuesday, September 30, 2008 8:50 AM

Answers

  • You can not mix querying LINQ to SQL and LINQ to Objects in the same query.

    You will need to execute the LINQ to SQL query first and then query over that against your special property.  This may cause memory or performance overheads where filtering is not applied on the SQL and it returns many results.

    [)amien
    • Marked as answer by a_a Monday, May 25, 2009 8:12 AM
    Wednesday, October 1, 2008 4:55 AM
    Moderator