linq

query problem with linq: Unable to cast object

hello all, This particular query is giving me an error please tell where am I going wrong public IList <BC_FeedbackBy> GetFeedbackList() { int feedbackId = 0; string feedbackName = string.Empty; using (brandconnectionsEntities modelObject = new brandconnectionsEntities()) { ...

Linq many to many?

Say I have products and receipts in a many to many relation. Is there any way I can directly query product.receipts or receipt.products and get the Iqueryable without having to reference the join_table at all? ...

Is there a way of splitting a C# generic dictionary into multiple dictionaries?

I have a C# dictionary Dictionary<MyKey, MyValue> and I want to split this into a collection of Dictionary<MyKey, MyValue>, based on MyKey.KeyType. KeyType is an enumeration. Then I would be left with a dictionary containing key-value pairs where MyKey.KeyType = 1, and another dictionary where MyKey.KeyType = 2, and so on. Is there a ...

Is this a spurious warning when using LINQ to SQL?

Per the many examples of LINQ I've seen, I'm creating my own data context and tables using code similar to the one below: class MyDatabase : DataContext { public Table<Widget> Widgets; public Table<Car> Cars; public MyDatabase (string connection) : base(connection) { } } But for every table (Widgets, Cars, etc), I get the warn...

How does a strongly typed DataContext work?

This is an in-depth continuation of my question from earlier this morning, which I'm still stumped about. I'm using a strongly typed DataContext for my application and although it emits a warning, it magically works. How does it do this? Here's the code one would typically use to connect to a database using LINQ-to-SQL. class MyDatabas...

How to bind controls without data (NULL)?

Hi, I have a GridView (RadGrid) being bound with data using LINQ. In the LINQ query, I use DefaultIfEmpty because some records have empty values to a lookup table. In the real SQL query generated by LINQ, we can see LEFT OUTER JOINs, so even if some data is not provided to reference the table, records are returned. My problem is when ...

LINQ: Find all intersecting data, not just the unique values

I thought that I understood Intersect, but it turns out I was wrong. List<int> list1 = new List<int>() { 1, 2, 3, 2, 3}; List<int> list2 = new List<int>() { 2, 3, 4, 3, 4}; list1.Intersect(list2) => 2,3 //But what I want is: // => 2,3,2,3,2,3,3 I can figure a way like: var intersected = list1.Intersect(list2); var lis...

LINQ to Stored Procedure. Can I call the SP with a dynamic list of parameters instead of strongly typing them?

I'm trying to make my code more dynamic. So I currently call the a stored procedure, with some strongly typed parameters like this var results = datacontext.spReturnUsers(txtUserId.Text, txtUserFirstName.Text, txtUserLastName.Text); Now what I would like to be able to do is loop through and add the parameters dynamically. So if tomo...

Convert anonymous type to array or arraylist. Can it be done.

Trying to retrieve data using linq from a database. I would like to use anonymous types and convert to an Ilist, Array, ArrayList or Collection. The data is used in a third party object that accepts Ilist, arraylist or collections. I can't seem to get this to work. I get the following error, "Sequence operators not supported for type ...

nested linq queries

I'm working with an xml file like this. <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt; <Body> <Element1> <Element2 day="2009-10-18"> <Element3 name="Joe"> <Element4 time="1"> <Element5 amount="0" price="16.58"/> <Element5 amount="1" price="18.58"/> <Eleme...

parse lines using linq to txt

var t1 = from line in File.ReadAllLines(@"alkahf.txt") let item = line.Split(new string[] {". "}, StringSplitOptions.RemoveEmptyEntries) let verse = line.Split(new string[] { "\n. " }, StringSplitOptions.RemoveEmptyEntries) select new { ...

Best way to retrieve XML data in MVC?

If I am passing an XElement or XDocument to my ASP.NET MVC view, what's the best (read: easiest) way to repopulate my XML with the values returned from the edit form on submit? Thanks, Matt. ...

Is IOrderedEnumerable<T> an inherently immutable collection? Should/can I yield?

I may not quite have my head around the IOrderedEnumerable<T> collection. If I have a field, my_field of type IOrderedEnumerable<T>, and I wish to return it from a public property getter, can I simply return my_field;? I'm used to yielding results to an IEnumerable<T> to prevent external modification to an internally held collection, bu...

Linq contains confusion

Hi, I have noticed something odd with linq and the Contains method. It seems to get confused on which Contains method to call. if (myString.Contains(strVar, StringComparison.OrdinalIgnoreCase)) { // Code here } The above code doesn't compile with the following error: The type arguments for method 'System.Linq.En...

LINQ query returns way more results than in the entire database

I have the following LINQ query. The problem is it's returning 13k results when tblSurveys only has 20 total. What am I doing wrong? from s in surveyContext.tblSurveys from st in surveyContext.tblTypes_for_Surveys from t in surveyContext.tblSurvey_Types where (s.Survey_Date >= startDate && s.Survey_Date <= stopDate) && (s.Unsubst...

When should I use LINQ for C#?

Hi, I'm learning C#, and I find LINQ absolutely interesting. However what is boggling me is, I can't think of a scenario whereby using LINQ would be an immense help, as its really not that hard replicating LINQ features in code. Any personal experiences/suggestions you might wanna share? Thanks! ...

Is it possible to construct a stringbuilder using a lambda function instead of foreach?

Possible Duplicate: LINQ to append to a StringBuilder from a String[] Forgive my functional programming noobiness, but is it even possible to use a lamba function to append each string in an array to a StringBuilder object? Is it possible to turn this code: // string[] errors = ... StringBuilder sb = new StringBuilder("<ul...

Is there any way to delay- execute a delegate against an IQueryable<T> after/during execution?

I expose an IQueryable method from my business layer for use in other layers. I would like to execute a function against each of the items in the enumeration, once the query has executed down-level. It seems like there should be an event that is raised after the query executes, so that I can then operate on the results from this common...

C# linq/lambda expression: How to select an integer out of a string?

I think the best way to explain my question is with a short (generic) linq-to-objects code sample: IEnumerable<string> ReadLines(string filename) { string line; using (var rdr = new StreamReader(filename)) while ( (line = rdr.ReadLine()) != null) yield return line; } IEnumerable<int> XValuesFromFile(string f...

How to Iterate over an XML Element result set from a Linq Query

This is my idea for the code (can the Linq query populate the dictionary object directly?). XElement doc = XElement.Load(data.xml); string Element = "KeyNode"; string SearchString = "Dominion"; Dictionary<string, string> QueryData = new Dictionary<string, string>(); var query = from child in doc.Descendants(Element) where S...