Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi everybody i have a problem with linq :
i have a class and into my class there is a precedure as IQueryable return
in procedure there is a simple join with linq and i have a foreach.
C#
public static class Class1
{
	public static IQueryable Testlinq()
	{
		var db = new DataClassesDataContext();
		var query = from tit in db.titles
			   join autit in db.titleauthors 
                           on tit.title_id equals autit.title_id
			   select new
			{
			   tit.publisher,
			   tit.price,
			   autit.author
			};
		foreach (var item in query)
		{
		  var tmp = item.author + " " + item.price + " " + item.publisher;
		}
		return query;
	}
	
}

foreach worked perfect here. but copy of this foreach to another file for examlpe default.aspx.cs doesn't work:
C#
var query = Class1.Testlinq();
		foreach (var item in query)
		{
		 var tmp = item.author + " " + item.price + " " + item.publisher;
		}

author and price and publisher are unknown here .
what must do i these items are known here?
thanks in advance
Posted
Updated 6-Feb-12 21:40pm
v2

You should not return anonymous types from Testlinq

Please read my answer at this page :
I have a problem in Linq to SQL to return a query? in method[^]

And then make the helper class and return IEnumerable<helperclassname></helperclassname> from your Testlinq() method.

You will have access to the HelperClass fields in your foreach body then.

Hope it helps.
 
Share this answer
 
Hi,

You can't do that. Method can't return anonymous type.

Create class with author, publisher and price properties and then return them from your method.
That way your method will return known type and all will work.
Define class:
C#
public class YourClass
{
	public string Publisher {get; set;}
	public string Author {get; set;}
	public int Price {get; set;}
}

And change method to select and return your defined type:
C#
public static class Class1
{
	public static IQueryable<yourclass> Testlinq()
	{
		// ...
		select new YourClass
		{
	            Publisher = tit.publisher,
		    Price = tit.price,
		    Author = autit.author
		};
		foreach (var item in query)
		{
		  var tmp = item.author + " " + item.price + " " + item.publisher;
		}
		return query;
	}
	
}

Maybe there are some syntax errors but you will get the point...
 
Share this answer
 
v2
try this code

C#
    var query = Class1.Testlinq();

    foreach (var item in query)
{
   item.author = item.author + " " + item.price + " " + item.publisher;
}

HTML
 the item.author is replaced with this 
(item.author + " " + item.price + " " + item.publisher;)

 example output is
 
 zyck 100.00 sample2012
 
Share this answer
 
v2
Just for the sake of completeness:

you might use the dynamic "type" here. Kind of last resort, if you can not change the interface to something more appropriate as suggested in Solution #2 and #3 of this thread.

C#
foreach (dynamic item in query)
{
    var tmp = item.author + " " + item.price + " " + item.publisher;
}


You will expereience a performance drop here:
Each member access to the dynamic variable item will inspect the type and search for the requested member before executing it!

That's why last resort ;-)

Cheers

Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900