linq

How do I select XML elements into a strongly-typed collection using LINQ?

I am trying to wrap my brain around using LINQ as a means of working with XML data and I'm having a problem figuring out the best way to select elements into a strongly-typed collection. I'm sure there's some minute detail I'm missing, but I can't seem to see it. Here is a sample of my XML File: <users> <User> <Use...

How to compare dictonary key with xml attribute value in xml using LINQ in c #?

Dear all, i've a dictonary " dictSample " which contains 1 data1 2 data2 3 data3 4 data4 and an xml file"sample.xml" in the form of: <node> <element id="1" value="val1"/> <element id="2" value="val2"/> <element id="3" value="val3"/> <element id="4" value="val4"/> <element id="5" value="val5"/> <element id="6" value="val6"/> <...

LINQ2SQL: orderby note.hasChildren(), name ascending

I have a hierarchical data structure which I'm displaying in a webpage as a treeview. I want to data to be ordered to first show nodes ordered alphabetically which have no children, then under these nodes ordered alphabetically which have children. Currently I'm ordering all nodes in one group, which means nodes with children appear nex...

Trigger on database using a web application and a winform application

Hello all, Situation: I have a web application which shows errors and where you can accept those error messages. I also have a service, which checks errors from a system and sets the error messages in the database. When I accept an error in the web application, i would like the service to know which error message has been accepted, so t...

Calling SubmitChanges on DataContext does not update database.

In C# ASP.NET MVC application I use Link to SQL to provide data for my application. I have got simple database schema like this: In my controller class I reference this data context called Model (as you can see on the right side of picture in properties) like this: private Model model = new Model(); I've got a table (List) of Series...

C# casting question: from IEnumerable to custom type

I have a custom class called Rows that implements IEnumerable<Row>. I often use LINQ queries on Rows instances: Rows rows = new Rows { row1, row2, row3 }; IEnumerable<Row> particularRows = rows.Where<Row>(row => condition); What I would like is to be able to do the following: Rows rows = new Rows { row1, row2, row3 }; Rows particula...

Problem with Unit testing of ASP.NET project (NullReferenceException when running the test)

Hi, I'm trying to create a bunch of MS visual studio unit tests for my n-tiered web app but for some reason I can't run those tests and I get the following error - "Object reference not set to an instance of an object" What I'm trying to do is testing of my data access layer where I use LINQ data context class to execute a cert...

Loop to LINQ Conversion -

Ok I have the following, set-up and working great. These lines of code should do a conversion from DAL Entity (Subsonic) to a ViewModel. IList<ProductOptionModel> OptionsRetData = new List<ProductOptionModel>(); foreach (var CurProductOption in this.ProductOptions) { OptionsRetData.Add(CurProductOption.ToDataModel(...

Use delegate for Projection in Linq to SQL

I have code something like this in an IRepository implementation in Linq to Sql: var newlist = from h in list where h.StringProp1 == "1" select new MyBusinessBO{ firstProp = h.StringProp1, secondProp = h.StringProp2 }; The projection into M...

Equivalent lambda syntax for linq query

Hi, I am having a mind-freeze here, but I cant seem to find the equivalent lambda syntax for - string[] a = {"a","b","c"}; string[] b = {"1","2","3"}; var x = from a1 in a from b1 in b select new{a1, b1}; ...

How do I pass a parameter from the select list into a function for joining a linq query?

I have a query that can be summarised in SQL as follows; Select S.StockCode From StockToCheck As S Inner Join GetPOSStock(S.StockCode) As C On S.StockCode = C.StockCode; I'm trying to do the same in Linq but it seems I cannot pass a parameter to the function I am joining on as it has not been parsed by Linq. I imagine it...

How to write this Linq-to-SQL select query

I have 3 tables: subcontracts, companies and contacts. Each table has a active_status flags which shows that the item is still active (hasn't been deleted). Each contact has a company_id field which stores the id of the contact's company. Each subcontract has a company_id field which stores the subcontract's company. Each compan...

Help with recursive linq expression

Hello Everybody, I have a database table where all the records are linked among each other i.e. something similar to the image bellow: As you can see on the diagram, a record can be root and can have 1 or more children, where each child stores in its ParentID property the ID property of its parent. I was wondering if someone can help...

Non Techie User Ad hoc LINQ query builder

Hi, I am looking for a third party component which allows a non technical user to create simple adhoc LINQ queries by dragging and dropping the available fields and run it against a data model that I will be populating using Entity Framework. I've used http://devtools.korzh.com/eq/dotnet/ component previously to generate adhoc SQL queri...

LINQ and set difference

I have two collections a and b. I would like to compute the set of items in either a or b, but not in both (a logical exclusive or). With LINQ, I can come up with this: IEnumerable<T> Delta<T>(IEnumerable<T> a, IEnumerable<T> b) { return a.Except (b).Union (b.Except (a)); } I wonder if there are other more efficient or more compac...

When using Query Syntax in C# "Enumeration yielded no results". How to retrieve output

I have created this query to fetch some result from database. Here is my table structure. What exaclty is happening. DtMapGuestDepartment as Table 1 DtDepartment as Table 2 Are being used var dept_list= from map in DtMapGuestDepartment.AsEnumerable() where map.Field<Nullable<long>>("GUEST_ID") =...

Querying a Single Column with LINQ

Hi Every one! I want to fetch array of values of a single column in a table, for example, I have a table named Customer(ID,Name), and want to fetch ids of all customers. my query in LINQ is: var ids = db.Customers.Select(c=>c.ID).ToList(); The answer of this query is correct, but I ran SQL Server Profiler, and saw the query which was...

Locking a table for getting MAX in LINQ

Hi Every one! I have a query in LINQ, I want to get MAX of Code of my table and increase it and insert new record with new Code. just like the IDENTITY feature of SQL Server, but here my Code column is char(5) where can be alphabets and numeric. My problem is when inserting a new row, two concurrent processes get max and insert an equa...

Why I am getting Null from this statement. Query Syntax in C#

This is not working. Returns Null to dept_list. var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable() where map.Field<Nullable<long>>("Guest_Id") == 174 select map.Field<Nullable<long>>("Department_id")).Distinct())as IEnumerable<DataRow>; DataTable dt = dept_l...

Tsql to Linq convertor

Is there any free software to convert Sql server queries to LINQ queries? i used Linqer, but its not free. Please suggest your opinions ...