Question

I’m new to PLINQO, I’m fairly new to LINQ2SQL and I’m adding a new DAL (called DAL2) in parallel with our old existing DAL. Our idea is to start using linq2sql for every new thing we add and slowly start moving old DAL usage to the new DAL2.

As we all know, DLINQ is nice for simple things but in more complicated scenarios (many2many, deattached objects, etc.) issues arise instantly and new need to hack around it. PLINQO (together with CodeSmish) comes to the rescue and adds all the necessary structure and tools to make it more usable. So far, so good.

Now I already have my DAL2 (I’m using Managers). It “works”. But I have a few doubts.

I understand that returning an object composed of the following query:

select name,  Count(*) as Total from SomeTable 

…is not a weird scenario in most DB applications. The query is a simple example of what would be a nice anonymous type.

Now for a second, imagine a table structure like this.

Tag (1) <—> (n) PatientTag (n) <—> (1) Patient

The idea is to know how many patients each tag has. A simple inner join would fix this with SQL, between tag and patient tag. I don’t even need access to Patient table for that. (I just need the count).

After all, all I want is for example:

TAG1, 33

TAG2, 21

TAG3, 6

etc…

Suppose I manage to create a linq2sql query that does it:

   var result1 = from pt in dc.PatientTag
                 join t in dc.Tag on pt.TagId equals t.TagId
                 select new { TagName = t.TagName };


   var result2 = from q in result1
                 group q by q.TagName into gp
                 select new { TagName = gp.Key, Total = gp.Count() };

(I’m new to LINQ so if the above is not good, forgive my lack of “LINQissm”)

Given that I cannot return that “new” anonymous type (unless I go for Object and reflection which is not my intention), I assume I have to create a “helper” class to contain the two things (name and total). The idea is that the above code should sit somewhere in the datalayer or the business layer, and not in the UI’s code.

Now the real question:

If the above is true, where should I create that “helper” if I want to return that result (or array of results) to a UI (for proper displaying and handling)?

1) In DAL2/Helper/TagNameTotal.cs (to put an example)

2) In a BLayer/Helpers/TagNameTotals.cs?

3) None of the above? (or either?)

If the above is not true, what am I incorrectly thinking?

Isn’t it normal when someone wants to pass the result of a query to the UI and modify it? Say in the above example I’d like to change TagName in the UI (maybe that’s not the best example but it applies).

Excuse me but I still find the whole LINQ2SQL to be a little bit crude when it’s used outside a webproject or a simple app. These are basic things we’ve been doing with ADO.NET (and recordsets before that) forever.

Make a select/join/group/crazySQL, modify, commit the changes back.

PLINQO is nice because it takes away the hassle of LINQ2SQL in its crude form (many 2 many, deatached, context regeneration, cache, etc.), but it’s still LINQ, so DLINQ practices must apply.

What am I missing?

note: Do not confuse PLinq with PLinqO.

Was it helpful?

Solution

I have answered this post in the codesmith community, however I was thinking about this and came across this post. You may find this post some help

Basically you create a mini class and use the let statement to define a sub set then lazy load it when you need it....

OTHER TIPS

This sounds to me like a shameless plug for the PLinqO product. I hope this is not your intent. I looked at PLinqO and perhaps would have got it except you had to purchase CodeSmith to get it. No thanks.

I have built a full featured 3-tier platform using L2S, and I encountered and dealt with issues related to M:M relationships, detached entities (especially with updates), context handling etc. without the use of PLinqO and any other 3rd party tool. It can be done, once you understand how L2S works. I think our solution is pretty elegant and our performance is quite good.

For those reading this, if you want to spend money on third party tools, invest in LinqPad and Linqer. You can pick up both of these tools for less than $75 total; they are fantastic. And I'm not associated with either of them.

Randy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top