LINQ SqlQuery< T>随机选择字段数 [英] LINQ SqlQuery<T> with random number of fields selected
问题描述
我正在执行运行时sql查询,并使用LINQ SqlQuery<>
I am making a run time sql query and getting data from database using LINQ SqlQuery<>
SchoolSoulLibrary.SchoolSoulDataEntities ss = new SchoolSoulLibrary.SchoolSoulDataEntities();
string query1;
var li = ss.Database.SqlQuery<MasterBank>(query1).ToList();
MasterBank类在哪里
where MasterBank class is
public Partial class MasterBank
{
public MasterBank()
{
}
public decimal BankId { get; set; }
public string BankName { get; set; }
public Nullable<decimal> UserId { get; set; }
public Nullable<decimal> SchoolId { get; set; }
}
如果我正在执行此查询
query1 = "Select * from MasterBank"; or
query1 = "Select BankId,BankName,UserId ,SchoolId from MasterBank";
它没有给出任何错误并返回所有数据
Its not giving any error and returning all data
但是如果我正在执行此查询
but if i am executing this query
query1 = "Select BankName,SchoolId from MasterBank";
发生错误
我了解此错误的原因,该错误返回类MasterBank类型的结果,但是现在我该怎么办,因为query1生成的运行时具有随机类MasterBank的属性.
除了 SqlQuery<> 之外,还有其他选择吗?
请建议
error occured
i understand the reason of this error that it return a result of the type of class MasterBank but what can i do now because query1 is generating runtime with random no of properties of class MasterBank.
is there any other alternative of doing it rather than SqlQuery<>
kindly suggest
推荐答案
在MasterBank类中,所有可选字段都需要标记为可选(可空).
All the fields that are optional need to be marked as optional (nullable) in MasterBank class.
这应该不会引起任何问题:
This one shouldn't cause any problems:
query1 = "Select BankId ,SchoolId from MasterBank";
因此,最快的解决方法是使所有字段为空.
So the quickest fix is just to make all your fields nullable.
这篇关于LINQ SqlQuery< T>随机选择字段数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!