Tag Archives: linq

Import Data from Excel to SQL Server DB using LINQ to Excel

Questions: I’m newbie in Linq to SQL and I want to import an Excel file contents to my SQL server Database. This is My Code : private void btnImport_Click(object sender, RoutedEventArgs e) { dbEntities = new BASUEntities(); string pathToExcelFile = importFileName; var excelFile = new ExcelQueryFactory(pathToExcelFile); excelFile.AddMapping<UserInfo>(x => x.FirstName, “FName”); excelFile.AddMapping<UserInfo>(x => x.LastName, “LName”); excelFile.AddMapping<UserInfo>(x… Read More »

How to call DB function from EF LINQ query?

Questions: In my database I have define some function, let’s say it’s called fnTest. Is it possible to call that function from my LINQ/EF query? Something like this: var param3, param4; var res = (from x in db.Something where x.field1 > 0 orderby fnTest(x.f1, x.f2, param3, param4) select x).Take(20); As you can see, I need… Read More »

Linq Query Returns Incorrect Result Set

Questions: I have a very complex Linq to SQL query that returns a result set from a Microsoft SQL Server database. The query is created using syntax similar to: Dim db as MyDataContext = MyGetDataContextHelper() Dim qry = From rslt in db.MyView Select ColumnList If userParam1 IsNot Nothing Then qry = qry.Where(lambda for the filter)… Read More »

What is SQL equivalent to LINQ .All()

Questions: What would this look like in SQL (SQL Server if you want to be particular)? // where people is a list of Person objects with property Name bool bobs = people.All(p => p.Name == “Bob”); Answers: You would check if there are any records that doesn’t match the criteria: not exists(select * from Persons… Read More »

Linq to Sql Count Include joins

Questions: I have a Linq to sql as following: var members=db.Members.Include(x=> x.Contact).Count(); Now due to some bad data all contacts in my members don’t have corresponding Contact record. So during the count how can i include the Count after Inner Join with the Contact table. The problem is when i get the list of members,… Read More »