我知道我不能写一个像这样的方法:
public var MyMethod()
{
return new{ Property1 = "test", Property2="test"};
}
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
public object MyMethod()
{
return new{ Property1 = "test", Property2="test"}
}
Run Code Online (Sandbox Code Playgroud)
但我不想做第二种选择,因为如果我这样做,我将不得不使用反射.
为什么我要这样做:
今天我在我的aspx页面中有一个方法返回一个数据表作为结果而我无法更改它,我试图将此DataTable转换为具有我想要使用的属性的Anonymous方法.我不想创建一个类只是为了这样做,因为我需要多次执行相同的查询,我想创建一个返回匿名类型的方法将是一个很好的意识形态.
可能重复:
LINQ to SQL:返回匿名类型?
我有一个标准的LINQ to SQL查询,它以匿名类型返回数据(包含大约6列各种数据类型的数据).
我想将此返回的对象提供给程序的其他部分,方法是将其返回给方法调用程序,或者将其分配给包含该方法的对象的属性.
如果它是匿名类型("var"),我怎么能这样做呢?
编辑 - 这是代码:
using (ormDataContext context = new ormDataContext(connStr))
{
var electionInfo = from t1 in context.elections
join t2 in context.election_status
on t1.statusID equals t2.statusID
select new { t1, t2 };
}
Run Code Online (Sandbox Code Playgroud) 我们都知道,当我们创建一个这样的匿名类时:
var Employee = new { ID = 5, Name= "Prashant" };
Run Code Online (Sandbox Code Playgroud)
...在运行时它将是类型:
<>f__AnonymousType0<int,string>
Run Code Online (Sandbox Code Playgroud)
有没有办法为这些类指定有意义的名称?
我想在控制台应用程序,VB.NET,VS 2010 Beta 2中使用HttpUtility.UrlEncode.
System.Web.HttpUtility.UrlEncode(item)
Run Code Online (Sandbox Code Playgroud)
错误消息:'HttpUtility'不是'Web'的成员.
在这个问题中, Anjisan建议添加对System.Web的引用,如下所示:
但是,我在该位置没有System.Web条目.
.net vb.net urlencode console-application visual-studio-2010
我可以在函数中使用匿名类型作为返回类型,然后将返回值的内容填充到某种数组或集合中,同时还向新数组/集合添加其他字段吗?请原谅我的伪代码......
private var GetRowGroups(string columnName)
{
var groupQuery = from table in _dataSetDataTable.AsEnumerable()
group table by new { column1 = table[columnName] }
into groupedTable
select new
{
groupName = groupedTable.Key.column1,
rowSpan = groupedTable.Count()
};
return groupQuery;
}
private void CreateListofRowGroups()
{
var RowGroupList = new List<????>();
RowGroupList.Add(GetRowGroups("col1"));
RowGroupList.Add(GetRowGroups("col2"));
RowGroupList.Add(GetRowGroups("col3"));
}
Run Code Online (Sandbox Code Playgroud) 我有一个Linq查询,我想从多个地方调用:
var myData = from a in db.MyTable
where a.MyValue == "A"
select new {
a.Key,
a.MyValue
};
Run Code Online (Sandbox Code Playgroud)
如何创建方法,将此代码放入其中,然后调用它?
public ??? GetSomeData()
{
// my Linq query
}
Run Code Online (Sandbox Code Playgroud) 我有两个表A和B.我可以触发Linq查询并获取各个表所需的数据.我知道每个表将返回的内容如示例所示.但是,当我加入两个表时我都不知道Linq查询的返回类型.这个问题可以通过创建一个将在其中包含ID,Name和Address属性的类来解决.但是,每次在根据返回类型编写连接查询之前,我将不得不创建一个不方便的类的方法是否有任何其他方法可用于实现此目的
private IList<A> GetA()
{
var query = from a in objA
select a;
return query.ToList();
}
private IList<B> GetB()
{
var query = from b in objB
select b;
return query.ToList();
}
private IList<**returnType**?> GetJoinAAndB()
{
var query = from a in objA
join b in objB
on a.ID equals b.AID
select new { a.ID, a.Name, b.Address };
return query.ToList();
}
Run Code Online (Sandbox Code Playgroud) 嗨,我需要找到一种方法来声明一个方法的匿名类型.这是我的代码:
public List<var> ListOfProducts(string subcategory)
{
var products = (from p in dataContext.Products
join s in dataContext.SubCategories.Where(x => x.SubCatName == subcategory)
on p.SubcatId equals s.SubCatId
join b in dataContext.Brands on p.BrandId equals b.BrandId
select new
{
Subcategory = s.SubCatName,
Brand = b.BrandName,
p.ProductName,
p.ProductPrice
});
return products;
}
Run Code Online (Sandbox Code Playgroud)
我不知道我应该为该方法设置List的类型.在这种情况下我该怎么办?
我如何以匿名类型返回列表,因为我得到了这个代码
"找不到类型或命名空间名称'T'(您是否缺少using指令或程序集引用?)"
我只需要返回IdMember和UserName,谢谢
public static List<T> GetMembersItems(string ProjectGuid)
{
using (PMEntities context = new PMEntities("name=PMEntities"))
{
var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information")
.Where(p => p.Knowledge_Project.Guid == ProjectGuid)
.Select(row => new { IdMember = row.IdMember, UserName = row.Profile_Information.UserName });
return items.ToList();
}
}
Run Code Online (Sandbox Code Playgroud) 我已经看到很多LINQ的例子,其中包含一个简单的对象列表:
var intList= new List<int>() { 1, 2, 3 };
var result = db.TableRecords.Where(c => intList.Contains(c.RecordId)).ToList();
Run Code Online (Sandbox Code Playgroud)
我想要做的似乎稍微复杂一点(我认为).我有一行类似于这个的代码获取我需要的列表:
var xzList = db.Relations.Where(r => someOtherList.Contains(r.zId))
.Select(r => new { AId = r.xId, BId = r.zId })
.ToList();
Run Code Online (Sandbox Code Playgroud)
现在我想得到与上一个示例类似的结果,但现在列表中有一个匿名类型,有两个整数.因此,如何将我现在得到result其中RecordId在TableRecords等于AId在匿名类型中的每个匿名类型xzList?
c# ×8
linq ×7
.net ×4
asp.net ×1
c#-3.0 ×1
collections ×1
generics ×1
linq-to-sql ×1
urlencode ×1
vb.net ×1