在浏览ASP.NET MVC文档时,我看到这个习惯用法很多:
new { foo = "bar", baz = "foo" }
Run Code Online (Sandbox Code Playgroud)
这是字典文字语法吗?它是一个新的类/结构,其类型由被调用的函数定义推断出来?如果它是怎样的变量不需要类型定义,甚至不是var?
这是一个基本问题.我已经设置了基本的SL4/RIA项目,我想在域服务中创建一个新方法并从中返回一些数据.我不确定这样做的最简单的方法..我应该把它包装在ToList()中吗?我不清楚如何处理这个创建的匿名类型..什么是返回这些数据最简单的方法?
public IQueryable<ApplicationLog> GetApplicationLogsGrouped()
{
var x = from c in ObjectContext.ApplicationLogs
let dt = c.LogDate
group c by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into mygroup
select new { aaa = mygroup.Key, ProductCount = mygroup.Count() };
return x;
// return this.ObjectContext.ApplicationLogs.Where(r => r.ApplicationID < 50);
}
Run Code Online (Sandbox Code Playgroud)
无法将类型'System.Linq.IQueryable <AnonymousType#1>'隐式转换为'System.Linq.IQueryable <CapRep4.Web.ApplicationLog>'.存在显式转换(您是否错过了演员?)58 20 CapRep4.Web
我使用两个DataContext对象返回单独的AsQueriable()数据集,然后使用linq连接两个.数据构造工作完美,但是当我将组合数据集传递给视图时,我得到的错误"对象"不包含"名称"的定义.
在调试会话期间,我可以清楚地看到父模型和foreach循环中的每个"项"都具有可见/可访问的所有数据和键.我很困惑.
stackoverflow.com上与此问题相关的许多其他q&a都没有解决我的问题,因此会欣赏一双新的眼睛并希望能够解决这个问题.
非常感谢! - 代码时间:
数据构建
public ActionResult SplashImages()
{
var g = (from i in GetGallerySplash() join o in GetFestivals() on i.Festival equals o.ID orderby i.Rating descending select new {i.Photo, i.OwnedBy, i.Rating, o.Name });
Response.ContentType = "text/xml";
return View(g);
}
private IEnumerable<Gallery> GetGallerySplash()
{
GallerysDataContext gdc = new GallerysDataContext();
return (from i in gdc.Galleries orderby i.Rating descending select i).Take(15).AsQueryable();
}
private IEnumerable<Festival> GetFestivals()
{
FestivalsDataContext fdc = new FestivalsDataContext();
return (from i in fdc.Festivals select i).AsQueryable();
}
Run Code Online (Sandbox Code Playgroud)
VSExpress的错误屏幕: …
我的代码返回一个对象:
public async Task<IHttpActionResult> GetAnswers(int userTestQuestionId)
{
return Ok(new AnswerToClientDTO
{
AnswerGridCorrect = answerGridCorrect,
Result = result,
UpdateRowCount = updateRowCount
});
Run Code Online (Sandbox Code Playgroud)
这是AnswerToClientDTO的代码,它仅在我的应用程序中的一个位置使用:
public class AnswerToClientDTO
{
public string AnswerGridCorrect { get; set; }
public int UpdateRowCount { get; set; }
public string Result { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我是否可以返回一个匿名对象,我不必从ASP.NET WEb API方法声明一个类?
我有一个函数创建一个本地List<object>,其中对象是一个匿名类型.我需要返回这些结果Dictionary<string, SortedList<DateTime, double>>.
数据列表如下所示.
{ Security = "6752 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 2 }
{ Security = "6754 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 3 }
{ Security = "6752 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 2 }
{ Security …Run Code Online (Sandbox Code Playgroud) 可能重复:
LINQ to SQL:返回匿名类型?
我在linq联接上对List <>使用什么返回类型?我要加入下面的多个表,但出现错误'Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.List<CargoTanksDAL.aspnet_Users>
public List<> GetAspnetUsersWithMembershipAndCompany()
{
using (DevCargoEntities db = new DevCargoEntities())
{
var users = from u in db.aspnet_Users
join mem in db.aspnet_Membership on u.UserId equals mem.UserId
join cl in db.CT_CompanyLogIn on u.UserName equals cl.UserLogIn
join companies in db.CT_Companies on cl.CompanyID equals companies.CompanyID
select new
{
u.UserId,
u.UserName,
mem.Email,
mem.IsLockedOut,
mem.IsApproved,
mem.CreateDate,
companies.CompanyName
};
return users.ToList();
}
}
Run Code Online (Sandbox Code Playgroud) 我想在C#中使用模块化编程.我试图用Linq查询创建一个方法.据我所知,我需要将Linq查询存储在变量中以便执行它.我创建了一个名为的类和方法SearchStudent(),但我不知道如何返回Linq查询.我该怎么办?
public var SearchStudent(string ogrenci_id)
{
var query =
from d in context.ogrencis
where d.ogrenci_id ==Convert.ToInt32(ogrenci_id)
select new
{
d.ogrenci_adi,
d.ogrenci_soyadi
};
return query;
}
Run Code Online (Sandbox Code Playgroud) 我不确定我是否正确地走这条路.我有一个Silverlight应用程序,并使用实体框架的很多.我有两个实体映射到我的数据库:标题和详细信息.我想提交一个左外联接来获取所有标题和详细信息 - 即使标题记录没有详细记录.这是我想从客户端运行的Linq查询:
var query =
from head in storeContext.Headers
join detail in storeContext.Details
on head.HeadId equals details.HeadId
into group
select new
{
Desc = head.Description,
MyCount = group.Count()
};
Run Code Online (Sandbox Code Playgroud)
由于这是Silverlight,我需要构建我的查询,然后使用storeContext.Load<T>()我的域服务(客户端上下文)中的方法将其提交给服务器.因为这个方法期待一个类型,我不知道如何构建调用以返回匿名类型,因为我在上面?
我做错了吗?我应该在这样的事情上使用Invoke方法吗?如果是这样,我如何定义我想要返回的类型?
是否有人可以指出我正确的方向,我真的会批评它.
谢谢......斯科特
c# silverlight linq-to-entities entity-framework anonymous-types
可能重复:
LINQ to SQL:返回匿名类型?
这是我的代码:
class B
{
public int age { get; set; }
public string name { get; set; }
}
class Program
{
static List<whatshouldIwritehere> GetList()
{
List<B> list = new List<B>() {
new B(){ age = 10, name = "jaagu" },
new B(){ age = 20, name = "juggu" },
new B(){ age = 30, name = "jockey" },
new B(){ age = 40, name = "jaggu" },
};
return (from item in list
select new …Run Code Online (Sandbox Code Playgroud) 以下是我的linq表达式:
return from c in db.RecipeIngredients
join d in db.Ingredients on c.IngredientID equals d.IngredientsID
where c.RecipeID.Equals(recipeID)
select (d.IngredientsID,c.Unit,c.IngredientID,c.Amount).ToList();
Run Code Online (Sandbox Code Playgroud)
这应该返回成分信息行的列表。返回的类型是什么?现在如何继续使用firstRow.ingredientName等?