linq to sql report tables in query - c#

Here's the method i want to write:
public static IEnumerable<String> GetTableNames(this IQueryable<T> query)
{
//...
}
where the IQueryable is a linq-to-sql query (is there a more specific interface i should use?).
then if i had a query like this
var q = from c in db.Customers
from p in db.Products
from cp in db.CustomerProducts
where c.ID = 3 && cp.CustID == c.ID && p.ID == cp.ProdID
select new {p.Name, p.Version};
q.GetTableNames();// return ["Customers", "Products", "CustomerProducts"]
basically it would show all the tables that this query touches in the db, it is ok to execute the query to figure this out too (since that is going to happen anyway)? any ideas?
(EDIT: sorry if this is a little too "give me teh codez!", any tips, partial solutions, or explanations of why this is impossible will be considered Thanks! -Luke)

LINQ-to-SQL allows you to capture the TSQL easily - just set .Log. The problem then is parsing that. I would be tempted to capture the TSQL, and play it back in SSMS with SET STATISTICS IO ON - this gives easily parsed per-table results.

You could do DataContext.GetCommand(query).CommandText, then parse that looking for FROM and JOIN clauses (tricky would be cartesian (comma) joins).

Related

Multiple joins with multiple on statements using Linq Lambda expressions [duplicate]

Suppose I have a list of {City, State}. It originally came from the database, and I have LocationID, but by now I loaded it into memory. Suppose I also have a table of fast food restaurants that has City and State as part of the record. I need to get a list of establishments that match city and state.
NOTE: I try to describe a simplified scenario; my business domain is completely different.
I came up with the following LINQ solution:
var establishments = from r in restaurants
from l in locations
where l.LocationId == id &&
l.City == r.City &&
l.State == r.State
select r
and I feel there must be something better. For starters, I already have City/State in memory - so to go back to the database only to have a join seems very inefficient. I am looking for some way to say {r.City, r.State} match Any(MyList) where MyList is my collection of City/State.
UPDATE
I tried to update based on suggestion below:
List<CityState> myCityStates = ...;
var establishments =
from r in restaurants
join l in myCityStates
on new { r.City, r.State } equals new { l.City, l.State } into gls
select r;
and I got the following compile error:
Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
UPDATE 2
Compiler didn't like anonymous class in the join. I made it explicit and it stopped complaining. I'll see if it actually works in the morning...
It seems to me that you need this:
var establishments =
from r in restaurants
join l in locations.Where(x => x.LocationId == id)
on new { r.City, r.State } equals new { l.City, l.State } into gls
select r;
Well, there isn't a lot more that you can do, as long as you rely on a table lookup, the only thing you can do to speed up things is to put an index on City and State.
The linq statement has to translate into a valid SQL Statement, where "Any" would translate to something like :
SELECT * FROM Restaurants where City in ('...all cities')
I dont know if other ORM's give better performance for these types of scenarios that EF, but it might be worth investigating. EF has never had a rumor for being fast on reads.
Edit: You can also do this:
List<string> names = new List { "John", "Max", "Pete" };
bool has = customers.Any(cus => names.Contains(cus.FirstName));
this will produce the necessary IN('value1', 'value2' ...) functionality that you were looking for

Join vs Navigation property for sub lists in Entity Framework

I have a sql statement like this:
DECLARE #destinations table(destinationId int)
INSERT INTO #destinations
VALUES (414),(416)
SELECT *
FROM GroupOrder grp (NOLOCK)
JOIN DestinationGroupItem destItem (NOLOCK)
ON destItem.GroupOrderId = grp.GroupOrderId
JOIN #destinations dests
ON destItem.DestinationId = dests.destinationId
WHERE OrderId = 5662
I am using entity framework and I am having a hard time getting this query into Linq. (The only reason I wrote the query above was to help me conceptualize what I was looking for.)
I have an IQueryable of GroupOrder entities and a List of integers that are my destinations.
After looking at this I realize that I can probably just do two joins (like my SQL query) and get to what I want.
But it seems a bit odd to do that because a GroupOrder object already has a list of DestinationGroupItem objects on it.
I am a bit confused how to use the Navigation property on the GroupOrder when I have an IQueryable listing of GroupOrders.
Also, if possible, I would like to do this in one trip to the database. (I think I could do a few foreach loops to get this done, but it would not be as efficient as a single IQueryable run to the database.)
NOTE: I prefer fluent linq syntax over the query linq syntax. But beggars can't be choosers so I will take whatever I can get.
If you already have the DestinationGroupItem as a Navigation-property, then you already have your SQL-JOIN equivalent - example. Load the related entities with Include. Use List's Contains extension method to see if the desired DestinationId(s) is(are) hit:
var destinations = new List<int> { 414, 416 };
var query = from order in GroupOrder.Include(o => o.DestinationGroupItem) // this is the join via the navigation property
where order.OrderId == 5662 && destinations.Contain(order.DestinationGroupItem.DestinationId)
select order;
// OR
var query = dataContext.GroupOrder
.Include(o => o.DestinationGroupItem)
.Where(order => order.OrderId == 5662 && destinations.Contain(order.DestinationGroupItem.DestinationId));

join using lambda expressions and retrieving the data

I have a members table with columns
member_Id,
member_Lastname,
member_Firstname,
member_Postcode,
member_Reference,
member_CardNum,
and i have another table mshipoptions with columns
mshipoption_id
mshiptype_id
and i have another table mshiptypes
mshiptype_id
mshiptype_name
another table memtomship
memtomship_id
mshipoption_id
member_id
and my entity name is eclipse
at the form load i am filling the datagrid view by using the below method....
private void reportmembers()
{
MemberControlHelper.Fillmembershiptypes(cbGEMembershiptype);
var membersreport = from tsgentity in eclipse.members
join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id
join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
select
new {
tsgentity.member_Id,
tsgentity.member_Lastname,
tsgentity.member_Firstname,
tsgentity.member_Postcode,
tsgentity.member_Reference,
tsgentity.member_CardNum,
mshiptypes.mshipType_Name,
};
if (txtfirstname.Text != "")
{
dgvmembersrep.DataSource = membersreport.Where(t => t.member_Firstname == txtlastname.Text).ToList();
}
if (txtcardnum.Text != "")
{
dgvmembersrep.DataSource = membersreport.Where(a => a.member_CardNum == txtcardnum.Text).ToList();
}
}
that was fine,...
My problem is here , i have one comboboxsay (cbgemembershiptype)......
when ever the user select the membership type in (cbgemembershiptype) i want retrieve the details of members those who have that membership type....
(Your question is unclear in terms of whether the query you've got already does what you want. I'm assuming it does.)
It's unclear why you'd want to convert this query into one using lambda expressions. It's certainly possible, but each join would introduce a new range variable - by the end, the strictly literal translation would end up with a select involving something like
member_Id = a.b.c.member_Id
... it wouldn't be very readable at all.
There are ways of improving it if you're writing it out by hand, but it still wouldn't be as clear as the query expression.
You should definitely know both forms, and write using whichever form is clearest for the query in question - which in this case is definitely the query expression form.
For more information about how query expressions are translated, see my Edulinq post on that topic.

Suggestions for designing complex LINQ code

What are your suggestions for designing linq code in project?
Especially, I`m interesting in code design of big and complex linq queries?
For example, you know, that you need to write a lot of huge linq stuff, maybe some of your code will have duplicate parts, maybe not, and you need:
Make the code easily supportive - means, if you need to change something . - you are changing one thing, not many
Make the code easy to read - means, if you need to find something - you easily doing this.
You can use your examples, maybe your practice. Maybe some patterns, that you saw anywhere - anything.
Saying linq I mean any linq, linq to sql, linq to objects, linq to xml, etc.
you can write extensions for your objects;
Main code;
IQuerable itemList = _coreRepository.GetSomeItems()
.WithStates(OperationIdentity.SendToSomeWhere)
.BetweenDates(StartDate, EndDate);
Extension;
public static IQueryable<SomeObject> BetweenDates(this IQueryable<SomeObject> query, DateTime startDate, DateTime endDate)
{
return from p in query
where p.CompletedDate >= startDate && p.CompletedDate < endDate
select p;
}
I like to put aside big Select statements that are used many times using extension methods.
public static IQueryable<SomeEntityPM> SelectEntityPM(this IQueryable<SomeEntity> entities)
{
return entities.Select(a => new SomeEntityPM { ... });
}
Usage:
ObjectCtx.SomeEntities.SelectEntityPM();
One useful pattern for this is creating a reusable predicate library. Check out this page on the LINQ PredicateBuilder for more info.
A vew things I often do:
1) Layout: always start a query at the next line. Example:
Don't do this
var allCustomersThatDontContainUnpayedOrders = from customer in db.Customers
where customer.Orders ...
select customer;
But do this:
var allCustomersThatDontContainUnpayedOrders =
from customer in db.Customers
where customer.Orders ...
select customer;
2) Use multiple where clauses where you can. I try to find the second snippet more readable than the first:
var results =
from customer in db.Customers
where customer.Name.Contains(search) && customer.Address.City != null &&
customer.Employee.IsSenior
select customer;
var results =
from customer in db.Customers
where customer.Name.Contains(search)
where customer.Address.City != null
where customer.Employee.IsSenior
select customer;
3) Prevent joins if you can. LINQ to SQL often allows you to just 'dot' over all parent-child relations without using hard-to-understand join statements.
4) Often you will see that many queries look alike. You might always want to filter certain records based on rights of a user. You can extract this code in a method:
var customers = db.Customers;
customers = FilterBasedOnUserRights(customers, currentUser);
public static IQueryable<Customer> FilterBasedOnUserRights(
IQueryable<Customers customers, User currentUser)
{
return
from customer in customers
where [big complicated where clause]
select customer;
}

Sub Query in LINQ

I have an SQL Query as given below
SELECT ui.PageStyleCss
FROM UserImages ui
WHERE ui.UserImageId IN
( SELECT inv.UserImageId
FROM Invitation inv
JOIN InviteeEmails invEmails ON
inv.InviteID = invEmails.InviteID
WHERE invEmails.InviteGUID = #InviteGUID
)
How can I write this in LINQ?
Thanks
My wild guess is that you're using LINQ to SQL. It would be nice if you mentioned this, along with details of your model. Guessing at its structure...
var q = from ui in Context.UserImages
where ui.Invitations.Any(i => i.InviteeEmails.Any(e => e.InviteGuid = inviteGuid))
select ui.PageStyleCss;
from ui in db.UserImages
where (from inv in db.Invitations
join invEmails from InviteeEmails
on inv.InviteId equals invEmails.InviteId
where invEmails.InviteGUID == inviteGUID
select inv.UserImageId).Contains(ui.UserImageId)
select ui.PageStyleCss
(not sure if it compiles or not)
I have to assume there's a better way...this is pretty much a direct translation.

Categories

Resources