linq

Extension Method with dynamically built linq predicate

Hi there, I have this extension method. public static IQueryable<T> SearchBy<T>(this IQueryable<T> source, SearchCriteria criteria) { //throw an error if the source is null if (source == null) { throw new ArgumentNullException("source"); } ParameterExpression parameter = ...

dynamic Linq queries with Entity Framework

I am aware of few efforts in constructing Linq queries dynamically, such as this, and this. None seem to be ideal as I would like to avoid putting expressions in a string, and omitting a where if it is not needed. My main concern is that the query is optimized for the database, and dynamically omits unnecessary clauses whenever possi...

Is it possible to express this code in LINQ?

I'm reading a C# book for beginners, and in every end of the chapter, there are exercises to be answered based on the lessons tackled. One of those exercises goes this way: (not the exact wordings) Write a program that will accept an int as the array length, and the values for the array. Then will print: "0" if the array is not sorted ...

C# Linq - SET syntax from SQL, i.e. SET abc (a property of obj) WHERE xyz = true in IEnumerable<obj>

I have a collection of Obj's, I want to go through the collection, and set a property if a condition is true, so in normal world it would be: foreach (var o in obj) { if (o.SomeProperty == Something) { o.SomeOtherProperty = true; } } Anyway to do this, using Linq, to make it in a single line? ...

How to left join two Datables?

Here i my Scenario I Have First Datable :TableA SerialNumber PartNumber 001 A 002 B var TableA = (from p in ddata.AsEnumerable() join q in Select.AsEnumerable() on p.Field<string>("SerialNumber") equals q.Field<string>("SerialNumber") ...

Taking distinct item from list of datapoints

I have a list of Datapoints (List). e.g original list (1,2) (2,2) (1,3) (3,3) (4,3) (2,3) (5,4) I want a output list as (1,2) (2,2) (3,3) (4,3) (5,4) or (1,3) (3,3) (4,3) (2,3) (5,4) i.e I want to remove all the other points where X value duplicates. One approach that I have is to loop through all the point and take the current p...

MVC DropDownListFor Value Cannot Be Null

i have the code: <%: Html.DropDownListFor(model => model.CompanyName, new SelectList(ViewData["ActiveCompanies"] as IEnumerable, "Id", "Name"))%> the list is being populated properally with the names etc, but the selected item name is not being bound to the Model.CompanyName (it would be fine if the solution returns the id so i can ha...

LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation

Hello, everybody. I know, that this topic has been discussed yet. But, unfortunately, I didn't find any solution in existing answers.So, I have the next code: public List<List<string>> DataTableParser(IQueryable<T> queriable) { //I missed the unnecessary code return queriable.Select(SelectProperties).ToList(); ...

what is the difference between for (or) foreach loop and linq query in case of speed

i like to know difference retrieval from list using for (or) foreach loop and retrieval from list using linq query. specially in case of speed and other difference EXample: List A=new List() contains 10000 rows i need to copy filter some rows from list A which one better in case of speed am i go with for loop or linq query ...

SQL Server stored procedure and Linq-to-SQL

I'm working on a project right now where we have to implement all communications to the database via stored procedures. We are using Linq-to-SQL, so all the database entities are in place. My question is how you could write a stored procedure that returns an entity, if this is possible. Example, we validate a user when he's logging in w...

Not Equal in LINQ?

Table Reords: TableA SerialNUmber Status 001 OK 002 N/A TableB SerialNumber Status 001 OK 003 N/A var ret = (from p in TableA.AsEnumerable() join q in TableB.AsEnumerable() on p.Field<string>("SerialNumber") equals q.Field<string>("SerialNumber") sele...

Set operations using a specific comparison rule

I have two lists: List<User> collection1 = new List<User>(); List<User> collection2 = new List<User>(); 1) I have to get all items common to both of the lists using LINQ. However, the class User has a lot of properties and I just want to compare FirstName and LastName. 2) How can I get the items in collection1 but not in collection2 ...

Select last value in a month for all given IDs using LINQ

This question is the second part of another question of mine, but this time focused on LINQ-to-SQL. I have 2 tables, one containing meter IDs, and another containing measurements for some of the meters in the first table. This is the table structure: MeterConfig: MeterID (int) MeterNumber (char[16]) Type (char[25]) Readings: Mete...

Sum amount using Linq in <List<Dictionary<string, int>>

Hi All, I have a generic list declared as so: List<Dictionary<string, int>> sales; The string will be a product name and the int the number of units sold. I want to group by productname and sum the total sales. So i can see the number of units sold for each product. How would i do this using linq? Thanks! ...

LINQ GroupBy month

I am having trouble getting an IQueryable list of a (subsonic) object grouped by Month and Year. Basic view of the object... public partial class DatabaseObject { [SubSonicPrimaryKey] public int objectID { get; set; } public string Description { get; set; } public decimal Value { get; set; } public string Categor...

Formatting old Query to LINQ statement

The following query is something i inheritted from an old app that adds 2 attributes to an excel stored in the database. SELECT row_number() over(order by id) as num,[id] as mailsort, 0 as pages,[xmlRecord].query('/sst-statement/*') FROM dbo.RPA200_preproc as [sst-statement] where rpatype = 201 order by id for xml auto returns an xml...

How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?

Please help this Linq newbie! I'm creating a list inside my class under test, and I would like to use Moq to check the results. I can easily put together a Predicate which checks the results of the list. How do I then make that Predicate into an Expression? var myList = new List<int> {1, 2, 3}; Predicate<List<int>> myPredicate = (lis...

How can I filter a dictionary using LINQ and return it to a dictionary from the same type

Hi, I have the following dictionary: Dictionary<int,string> dic = new Dictionary<int,string>(); dic[1] = "A"; dic[2] = "B"; I want to filter the dictionary's items and reassign the result to the same variable: dic = dic.Where (p => p.Key == 1); How can I return the result as a dictionary from the same type [<int,string>] ? I trie...

Entity Framework NoTracking not working

I'm trying to run a no tracking query on my entities so that I can update them outside of the context. However, when the no tracking is not working and I get an exception stating "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." This exception is thrown by a property wh...

Zip N IEnumerable<T>s together? Iterate over them simultaneously?

I have:- IEnumerable<IEnumerable<T>> items; and I'd like to create:- IEnumerable<IEnumerable<T>> results; where the first item in "results" is an IEnumerable of the first item of each of the IEnumerables of "items", the second item in "results" is an IEnumerable of the second item of each of "items", etc. The IEnumerables aren't n...