A LINQ query implementation based on. NET MVC for alumni seven file uploads and Many-to-many relational tables

Source: Internet
Author: User
Tags current time datetime empty file upload join

Upload and call of pictures

Image upload is the file upload, in the foreground is type= "file" input, but to declare the form as Multipart/form-data mode, the method is written in BeginForm: @using (Html.BeginForm ( "Tocreate", "Class", FormMethod.Post, new {enctype = "multipart/form-data"}), define the type of enctype as an object multipart/ Form-data. In this way, the background can get file from the cache. Look backstage:

HttpPostedFileBase FC = Request.files[0];

if (FC!= null)

{

Newclass.classcoverpath =filehelper.savefile (FC);

}

The top is to extract the first file from request requests, and then put into the HttpPostedFileBase type FC, to determine whether FC is empty, because some users do not upload, this time is empty, do not perform the save operation, if not empty, Then the Filehelper.savefile (FC) is executed. This method is a tool class. The code is as follows:

String fullname = file. FileName;

String extname = FullName. Substring (fullname. LastIndexOf (".") + 1, fullname. Length-1-FullName. LastIndexOf ("."));

String newName = DateTime.Now.ToLongTimeString (). Replace ("/", ""). Replace (":", ""). Trim () + "." + Extname;

File. SaveAs (Path.GetFullPath ("E:/ensleep/documents/visual Studio 2012/projects/alumnibook/alumnibook/upload/") + NewName);

return newName;

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

The first is to get the file name from file (FC), then cut the file name, remove the extension, and then use DateTime.Now to get the current time, and then call the Tolongtimestring () method to grow the time string, where "/", ":" The illegal character is removed, then the extension is used as the new name for the file, and then the SaveAs method saves the file stream to the local upload folder, and finally returns the new name of the file. After the controller receives a new name, the new name to the entity, and then saved to the database, the next call, only need to take out the name of the picture, in front plus the path, you can directly reference.

A multi-table joint query for datasets implemented by LINQ

It took a lot of time for LINQ datasets to be queried, although the results were simple, but every person with a similar experience would know that the process was that long and difficult. After the user logs in, the personal center needs to display all the classes that the user is in, that is, the Ismyclass in the model MyClass.

public class MyClass

{

Public UserInfo MyInfo {get; set;}

Public list<class> Ismyclass {get; set;}

Public list<class> Notmyclass {get; set;}

}

This is a list type and is a collection of data. And then you need another list set, which is Notmyclass, where the user is in the class and not in the class. This problem seems very simple, in fact, the user and the class is linked to the table, that is, many-to-many relationship. A class can have multiple users, and a single user can have multiple classes. If you use the SQL statement of course good to write, online solutions have many, with the outreach query on it. But here, LINQ does not support outer join! so the problem comes out. The process is not said, let's say the result. Let's take a look at the Ismyclass query:

var query0 = from UC in db. User_class

Join U1 in Db. UserInfo on UC. UserName equals U1. UserName

Join C1 in DB. Class on UC. ClassName equals C1. ClassName

where U1. UserName = = UserName

Select C1;

This is a common left connection in SQL, this method is in LINQ, I will class, user two tables through User_class connected together, and then query, and finally remove class. This is OK, because and SQL about, but Notmyclass entangled, find ways to seek a variety of methods, but also consulted a few people who are writing books, they did not tune out, and finally recommended the use of a member of the collection type entity, you can let class as a member, put in the user. That is, declare a public list<class> class{get;set in user; Members, this issue at the beginning of the manual has said, is not stable, and the system will default to create a relational table, students do not recommend such use. So, finally thought of a way, is to discard the database, directly to the collection of memory, and then set operations, that is, all classes found to put in the Notmyclass:

Mc. Ismyclass = Query0. Tolist<class> ();

Mc. Notmyclass = db. Class.tolist ();

foreach (Class RC in MC. Ismyclass)

{

Mc. Notmyclass.remove (RC);

}

The entities in the Ismyclass are then traversed and removed from the Notmyclass. This is not the most primitive solution, but it is a thought, complex database queries, put in memory for collection processing, so intuitive and not error-prone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.