.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
MarieAdela
Imran Ghani
Post New Web Links

How to use SP with Linq to Entities?

Posted By:      Posted Date: September 25, 2010    Points: 0   Category :ASP.Net
 

Hi!

How to use Stored Procedures with Linq to Entities?
I prefer an example with input and output

The videos on asp.net only show how to do this with Linq to SQL.

Thank you!




View Complete Post


More Related Resource Links

Serializing Linq to Entities for use in a REST Service

  
What would be the best approach to send data retrieved from Linq to Entities over a rest service? For example, if you have a Linq to Entities class, say Products from the Northwind database. I want to return a subset of the Products (based on a query on the model) back through a service. First a retrieve a List<Product> from the context, and then i need to serialize the data and send it over the wire. If I use the exact Linq to Entities class, then there is so much "garbage" that it either fails outright (from a circular reference) or you are wasting bandwith. What is a design to overcome this? Create POCO objects for each linq to entities class you want to send and create custom code to tranform each object in the list to the POCO object? This is the only way I can see doing it, although it would be very tedius to create and maintain (unless some custom code generation was created). Is there a better approach? clients

Change connection string of Linq to Sql entities

  
Hi all.I have a solution which uses Linq to SQL Data Classes to acced the db. I have a project for my domain objects in my solution, which is a class library and has the dbml file. When I set up the dmbl using the wizard, Visual Studio created an app.config file on that project with the connection string. It also creatting a settings file with the same info. I've noticed that if I change the connection string in the settings file, the app.config gets updated, which is the expected behaviour.However, I'm having a problem when I deploy my web site to the production server. I already set the app.config file's Build Action to "Content", and also set Copy to ouput directory to "Always". Nevertheless, when I install the web site and change the connection string in the app.config file, it still takes the old connection string, so obviously I get an error. I've found that it only works if I change it with in Visual Studio and then compile, but of course that's not an optimal solution, because any change in the production db server would mean I have to change my project's assembly, thus generating a new version.Any thing I'm missing? What do I have to configure so that the assembly takes the connection string form my app.config file directly after I deploy?Thanks a lot.

Left Outer Join in LINQ + Entities

  
HiI have found quite a bit of info on the net, but I can't get it to work. Here's the normal SQL query:SELECT cl.ID AS ClientID, cl.Name, cl.Surname, CLC1.PHONECODE + ' ' + CLC1.PHONENUMBER AS WorkNum, CLC2.PHONECODE + ' ' + CLC2.PHONENUMBER AS HomeNum, CLC3.PHONECODE + ' ' + CLC3.PHONENUMBER AS CellNum, CLC1.PREFERRED AS WorkNumPref, CLC2.PREFERRED AS HomeNumPref, CLC3.PREFERRED AS CellNumPref, veh.ID AS VehicleID, veh.MAKE, veh.MODEL, veh.YEAR, veh.REGNO, ws.ID AS WorkshopID, ws.JOBTYPE, ws.SERVICEADVISOR, ws.TIMEFINISHED, ws.TECHNICIAN, fs.ID as FollowupID, fs.TYPE, fs.DESCRIPTION, fs.SCHEDULEDATE, fs.WRONGNUMBERFLAG FROM CLIENTS AS cl LEFT OUTER JOIN CLIENTCONTACTNUMBERS AS CLC1 ON cl.ID = CLC1.CLIENTID AND CLC1.TYPE = 'W' LEFT OUTER JOIN CLIENTCONTACTNUMBERS AS CLC2 ON cl.ID = CLC2.CLIENTID AND CLC2.TYPE = 'H' LEFT OUTER JOIN CLIENTCONTACTNUMBERS AS CLC3 ON cl.ID = CLC3.CLIENTID AND CLC3.TYPE = 'C' INNER JOIN CLIENTVEHICLES AS clveh ON cl.ID = clveh.CLIENTID INNER JOIN VEHICLES AS veh ON clveh.VEHICLEID = veh.ID INNER JOIN WSBOOKINGS AS ws ON clveh.ID = ws.CLIENTVEHICLEID INNER JOIN FOLLOWUPSCHEDULE AS fs ON fs.WSBOOKINGID = ws.ID WHERE fs.ID = 2 There is a multiple join between CLIENTS and CLIENTCONTACTNUMBERS so it would return a single row of data. If you were to do a normal join on CLIENTS and C

Linq to entities: Always select before delete and update?

  

Hi,

With linq to entities, how can I delete and update data inside a database without first selecting the complete entity at first?
The idea behind this is to improve performance.

Thanks!


a problem with Linq to Entities

  

hi

i have a query that get some data from database and i want to databind a drop down list control that have alredy an item in it

the code is:

Private Sub DatabindFamilyMembers()

        Dim db As New AghakhanianEntities()
        Dim members = From m In db.UserProfiles _
                     Where m.IsFamilyMember = True _
                     Select FullName = m.FirstName + m.MiddleName + m.LastName, m.ProfileID _
                     Order By FullName

        Dim i As Integer = 0
        Dim text As String
        Dim value As String
        Do While i <= members.Count()
            value = members(i).ProfileID
            text = CType(members(i).FullName, String)
            i = i + 1
            ddlParent.Items.Insert(i, New ListItem(text, value))

        Loop

        ddlParent.DataBind()
    End Sub


when i run it in line value=.... get some error like:

LINQ to Entities does not recognize the method 'VB$AnonymousType_2`2[System.String,System.Int32] ElementAtOrDefault[VB$AnonymousType_2`2](System.Linq.IQueryable`1[VB$AnonymousType_2`2[System.String,System.Int32]], Int32)' method, and this method cannot be translated into a store expression.

Linq to Entities Many to Many Persistence Issue

  

I am having problems persisting a many to many. My code saves the Problem but not the users that can be attached to a problem. My Linq to Entities Mapping is below in the first three code blocks. I then have my ASP MVC controller method which receives a problem, a file (optional), and a string array of users which are allowed to view the problem. I retrieve the Users from the UserRepository and add them to a List which is then passed through to the ProblemRespository with the problem and added to the problem.Users collection. However when submitted only the problem is being saved and not the records which are required in the junction table. Any help would be appreciated. This is the first time I've attempted my own mapping.
Thanks in advance.

Object Must Implement IConvertible error in LINQ to Entities with group by

  
1 down vote favorite
1

Hi. I have an ASP.NET MVC application in which i am using entity model for connecting to MySQl database

Here I am joining 2 tables and then applying group by on one of the fields. Then fetching the result from GroupBy result. Everything is working fine on development machine but getting  error:

"Object Must Implement IConvertible"   when executing on the stagging server.

Can you please help me out why such error is occurring. I have taken the dump of database form staging server and then used that on development machine then also working fine. But when I placed the dump of development machine database on staging then getting the same exception.

So where is problem i am not understanding.

The model structure is

student:
studentid
int
course
string
name
string

student_payment
:
id
int
studentid
int
amount
decimal

Here student and st

How can I get two tables in only one query with LINQ to Entities?

  

Code for example:

var context = new cEntities();

var query = from m in context.Users where m.UserId==2

select m from c in context.Folder where c.FolderId==5 select c;

 


I don't want to write var query twice and connect twice to DB, I want to do it one var query only.


Is this possible at all? Getting to different tables at once?


No connection between the tables, but I want to save DB connection.

 


 

 

 


Any opinions on using Projection or explicit join syntax when joining entities in Linq to SQL?

  

Folks,

I would like to know whether people use the join syntax OR projection when joining entities in LINQ to SQL queries. I have noticed that when using projection that a CROSS JOIN is in the generated SQL

e.g. 

var query =
from o in Orders
from  oi in o.OrderItems
where oi.OrderId == o.OrderId

generates SQL something like...

FROM [Customer].[Orders] AS [t0]
CROSS JOIN [Customer].[OrderItems] AS [t1]
WHERE ([t1].[OrderId] = [t0].[OrderId]) AND ([t1].[OrderId] = [t0].[OrderId])

whereas with explicit joins I get a clean inner join..

var query =
from o in Orders
join oi in OrderItems on o.OrderId equals oi.OrderId

generates..

FROM [Customer].[Orders] AS [t0]
INNER JOIN [Customer].[OrderItems] AS [t1] ON [t0].[OrderId] = [t1].[OrderId] 

I know using explicit joins over projection is frowned upon, but I am wary of those cross joins.

Just wondering what other folks preferences are when using joins in L2SQL.

Cheers

K

 

 


Custom database function cannot be translated into a LINQ to Entities store expression

  

Hi,

I'm trying to get a UDF from my SQL Server database to map to Linq to Entities. I've found an example for the CourseManager sample in the MSDN :

Private

 

 

Sub Button1_Click(

System.Linq.Enumerable+WhereListIterator`1[Web.Domain.Models.Currency.Entities.GetCurrencyRatesFromD

  

Hi

Can anyone see where i'm going wrong here as i get the following in my view:

System.Linq.Enumerable+WhereListIterator`1[Web.Domain.Models.Currency.Entities.GetCurrencyRatesFromDatabase]

My code is:

 

public ActionResult Index()
            {
            List<GetCurrencyCountriesDataFieldsFromDB> List1 = new List<GetCurrencyCountriesDataFieldsFromDB>();
            List<GetCurrencyRatesFromDatabase> List2 = new List<GetCurrencyRatesFromDatabase>();

            _IGCFDB.GetCurrencyFromDataBase(List1, List2);

            var rate = from c in List2 where c.DboCurrency.Contains("GBP") select c;

            this.ViewData["Test"] = rate;

            return View();
            }

 

and in view i have

@ViewBag.test

Thanks

George


Error: cannot be translated into a LINQ to Entities store expression

  

When I run my code i get this error:

The specified method '{0}' on the type '{1}' cannot be translated into a LINQ to Entities store expression

it happens in this part of the code:

 public Boolean valtOpVerlofDag(Dictionary<string, object> afw)
        {
            DateTime start = DateTime.Parse((string)afw["startDatum"]);
            IEnumerable<Period> holidays = from hol in periods
                                               where (hol.Type == "Holiday") && hol.IsInPeriod(start)
                                               select hol;
            if (holidays.Count() == 0)
          

Playing with Linq grouping: GroupByMany?

  
One of its features is grouping. Many people understand grouping like it is defined in Sql. Linq is implementing grouping quite the same way. Let's discover this syntax and how to make consecutive groups easier.

LINQ : Implementing IN and NOT IN

  
I got tried of typing

var result = from s in source
where items.Contains(s)
select s;and so I implemented the IN and NOT IN methods as extension methods:

101 LINQ Samples Tutorials

  
101 LINQ Example with sample code snippets....This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value

Linq: how to share parameters between lambda expressions ?

  
When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it's not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another.
The compiler is using two technical different ways to let parameters go out of a method.

Linq to Sql CompiledQuery container

  
Ok now let's go. Here is just a little trick but with some interesting patterns that could be useful in some other contexts not connected to Linq to Sql.

When using Linq expressions, like with Linq to Sql, translating the expression into something else (sql for example) is taking time and resources. Sometimes it's negligible, sometimes not...
Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend