none
Help refactor LinqToSQL Expressions RRS feed

  • Question

  • Hi,

     

    I have Dictionary that collects a bunch of queries. The queries are quite large in terms of lines and I'm gonna have about 10-15 of them and now my question is: Can any of you give me some hints to what I can do to refactor it in to some general methods... if it's at all possible. The first two expression look like this:

     

     

    Code Snippet

     

    // The first query is being added here....

    texts.Add(StatusTextGroup.CeilingAndRoof,

    getTextsForStatusTextGroup(StatusTextGroup.CeilingAndRoof,

    from bu in db.SingleBuildingParts

    join sortOrders in db.StatusTextOrder on

    new

    {

    BuildingId = (int)bu.BuildingId,

    Group = StatusTextGroup.CeilingAndRoof,

    Type = StatusTextType.Foreign,

    TextId = bu.SingleBuildingPartId

    }

    equals

    new

    {

    sortOrders.BuildingId,

    sortOrders.Group,

    sortOrders.Type,

    sortOrders.TextId

    }

    into statusTextOrders

    from statusTextOrder in statusTextOrders.DefaultIfEmpty()

    where bu.Type == BuildingPartType.CeilingAndRoof &&

    bu.SuggestionFor == null &&

    bu.TemplateType == null

    select new StatusTextItemConfig()

    {

    RecordId = bu.SingleBuildingPartId,

    Text = bu.LongText,

    DataKey = "LongText",

    ServicePath = "/Single/Envelope/BuildingParts/WebService.asmx",

    ServiceMethod = "save",

    IsForeign = true,

    SortOrder = statusTextOrder.SortOrder

    }));

     

     

    // The second query is being added here

    texts.Add(StatusTextGroup.Wall,

    getTextsForStatusTextGroup(StatusTextGroup.Wall,

    from bu in db.SingleBuildingParts

    join sortOrders in db.StatusTextOrder on

    new

    {

    BuildingId = (int)bu.BuildingId,

    Group = StatusTextGroup.CeilingAndRoof,

    Type = StatusTextType.Foreign,

    TextId = bu.SingleBuildingPartId

    }

    equals

    new

    {

    sortOrders.BuildingId,

    sortOrders.Group,

    sortOrders.Type,

    sortOrders.TextId

    }

    into statusTextOrders

    from statusTextOrder in statusTextOrders.DefaultIfEmpty()

    where bu.Type == BuildingPartType.Wall &&

    bu.SuggestionFor == null &&

    bu.TemplateType == null

    select new StatusTextItemConfig()

    {

    RecordId = bu.SingleBuildingPartId,

    Text = bu.LongText,

    DataKey = "LongText",

    ServicePath = "/Single/Envelope/BuildingParts/WebService.asmx",

    ServiceMethod = "save",

    IsForeign = true,

    SortOrder = statusTextOrder.SortOrder

    }));

     

     

    As you can see... There are quite a lot of duplication, but I can not figure out how to refactor it out... all ideas will be highly appreciated!

     

    Regards, Asger

    Tuesday, August 5, 2008 8:29 AM